0

So I created a model called SellPostImage so I can upload an image to the model which is the post called SellPost, and I have created two forms one called SellForm which would have the title, category etc. and another form that would handle the image uploading called SellPostImage. The SellPost has a Many-To-One relationship using the ForeignKey with User model and the SellPostImage has a OneToOneField relationship with the SellPost model (I have tried using ForeignKey on the SellPostImage model but that didn't change anything for me)

I keep getting the error

Exception Value: NOT NULL constraint failed: esouqbahrain_sellpost.user_id

whenever I try to submit the post filling out all the fields and choosing an image.

Here are my models:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    pictures = models.ImageField(upload_to='profile_images', blank=True)

    def __unicode__(self):
        return self.user.username

    class Meta:
        verbose_name_plural = "User Profiles"



# Create your models here.
class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    views = models.IntegerField(default=0)
    likes = models.IntegerField(default=0)
    slug = models.SlugField(unique=True, default='automatic')

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Category, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.name


    class Meta:
        verbose_name_plural = "Categories"

class SellPost(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=128)
    category = models.OneToOneField(Category)
    body = models.CharField(max_length=400)
    price = models.DecimalField(decimal_places=1, max_digits=5, default=0.0)
    views = models.IntegerField(default=0)
    likes = models.IntegerField(default=0)
    slug = AutoSlugField(populate_from='title', unique=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(SellPost, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.title

class SellPostImage(models.Model):
    post = models.OneToOneField(SellPost, null=True)
    pictures = models.ImageField(upload_to='post_images', blank=True)

    def __unicode__(self):
        return self.post.title

    class Meta:
        verbose_name_plural = "Post Images"

And here is my view for posting the forms:

@login_required
def sell(request):
    if request.method == 'POST':

        sell_form = SellForm(data=request.POST)
        image_form = SellPostImageForm(data=request.POST)


        if sell_form.is_valid() and image_form.is_valid():
            post = sell_form.save()
            post.save()
            img_form = image_form.save(commit=False)
            if 'picture' in request.FILES:
                img_form.pictures = request.FILES['picture']
            img_form.save()

    else:
        image_form = SellPostImageForm()
        sell_form = SellForm()
    return render(request, 'sell.html', {'sell_form': sell_form, 'image_form': image_form})

And here are the forms if anyone needs them:

class SellForm(forms.ModelForm):

    title = forms.CharField(max_length=128)
    category = forms.ModelChoiceField(queryset=Category.objects.all().order_by('name'))
    body = forms.CharField(max_length=400, widget=forms.Textarea)
    price = forms.DecimalField(initial=0.0)
    views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
    likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
    slug = forms.CharField(widget=forms.HiddenInput(), required=False)

    class Meta:
        model = SellPost
        fields = ('title', 'category', 'body', 'price',)

class SellPostImageForm(forms.ModelForm):
    class Meta:
        model = SellPostImage
        fields = ('pictures',)

Thank you in advance :)

qasimalbaqali
  • 1,825
  • 18
  • 45

1 Answers1

2

You're SellPost model requires a User. SellForm form has no user field. What are your post variables?

fiacre
  • 970
  • 1
  • 8
  • 25
  • What do you mean by post variables. Beginner here, sorry. And doesn't my SellPost already has a User which is 'user' ? – qasimalbaqali Jun 09 '15 at 01:50
  • I mean, when populating a form, one often used POST variables. See http://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get Your form never creates a user, but the database requires one because of the Foreign Key relation – fiacre Jun 09 '15 at 01:58
  • I fixed it thanks to you! I first removed the posts that have already been made and the images that have been uploaded visa SellPost, and SellPostImage and then tried to migrate which worked, and then I have added the User model into the SellPost model and that did the trick! – qasimalbaqali Jun 09 '15 at 11:54