简体   繁体   English

Django:“ NOT NULL约束失败”。user_id

[英]Django: “NOT NULL constraint failed” .user_id

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 . 因此,我创建了一个名为SellPostImage的模型,以便可以将图像上载到名为SellPost的帖子的模型中,并且我创建了两种形式,一种名为SellForm ,具有标题,类别等,另一种形式可以处理图像上载。称为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) SellPost具有使用一个多到一的关系ForeignKeyUser模型和SellPostImageOneToOneField与关系SellPost模型(我已经尝试使用ForeignKeySellPostImage模型,但并没有改变我的任何东西)

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 :) 先感谢您 :)

You're SellPost model requires a User . 您是SellPost模型,需要一个User SellForm form has no user field. SellForm表单没有用户字段。 What are your post variables? 您的帖子变量是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM