姜戈:"NOT NULL constraint failed" .user_id

Django: "NOT NULL constraint failed" .user_id

所以我创建了一个名为 SellPostImage 的模型,这样我就可以将图像上传到名为 SellPost 的 post 模型,并且我创建了两个表单,一个名为 SellForm 包含标题、类别等,另一种处理图像上传的表单称为 SellPostImageSellPost 使用 ForeignKeyUser 模型具有 Many-To-One 关系,SellPostImageSellPost 具有 OneToOneField 关系模型(我尝试在 SellPostImage 模型上使用 ForeignKey,但这对我没有任何改变)

我一直收到错误

Exception Value: NOT NULL constraint failed: esouqbahrain_sellpost.user_id

每当我尝试提交 post 填写所有字段并选择图片时。

这是我的模型:

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"

这是我对 post 表格的看法:

@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})

如果有人需要,这里有表格:

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',)

提前谢谢你:)

您是 SellPost 模特需要 UserSellForm 表单没有用户字段。你的 post 变量是什么?