![](/img/trans.png)
[英]null value in column “user_id” violates not-null constraint Django
[英]Django: “NOT NULL constraint failed” .user_id
因此,我創建了一個名為SellPostImage
的模型,以便可以將圖像上載到名為SellPost
的帖子的模型中,並且我創建了兩種形式,一種名為SellForm
,具有標題,類別等,另一種形式可以處理圖像上載。稱為SellPostImage
。 該SellPost
具有使用一個多到一的關系ForeignKey
與User
模型和SellPostImage
有OneToOneField
與關系SellPost
模型(我已經嘗試使用ForeignKey
上SellPostImage
模型,但並沒有改變我的任何東西)
我不斷收到錯誤
Exception Value: NOT NULL constraint failed: esouqbahrain_sellpost.user_id
每當我嘗試提交填寫所有字段並選擇圖片的帖子時。
這是我的模型:
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"
這是我發布表格的觀點:
@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
模型,需要一個User
。 SellForm
表單沒有用戶字段。 您的帖子變量是什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.