我IntegrityError
在尝试将模型保存到数据库时遇到问题
def done(self, form_list, **kwargs):
new = Business()
new.user = self.request.user
for form in form_list:
for k,v in form.cleaned_data.iteritems():
new.k = v
new.save()
return redirect('/')
错误说:(1048, "Column 'category_id' cannot be null")
正在完成并具有类别模型的表格:
class AddForm2(forms.Form):
category = forms.ModelChoiceField(queryset = Category.objects.filter(children__isnull=True))
city = forms.ModelChoiceField(queryset = City.objects.all())
name = forms.CharField(max_length = 200, label="Business Name")
address = forms.CharField(max_length = 200, label="Street Address")
slogan = forms.CharField(max_length=140)
phone = forms.CharField(max_length=10)
website = forms.URLField()
email = forms.EmailField()
商业模型.py
class Business(models.Model):
TYPE_CHOICES = (
('1','Basic'),
('2','Silver'),
('3','Platinum'))
user = models.OneToOneField(User)
ad_type = models.CharField(choices = TYPE_CHOICES, default = '1', max_length=1)
name = models.CharField(max_length=200)
slug = models.SlugField()
address = models.CharField(max_length=200)
slogan = models.CharField(max_length=140, blank=True)
description = models.TextField()
phone = models.CharField(max_length=10)
website = models.URLField(blank=True)
email = models.EmailField()
twitter = models.CharField(max_length=100, blank=True)
facebook = models.URLField(blank=True)
category = models.ForeignKey(Category)
city = models.ForeignKey(City)
编辑:
我在我的 done 方法中更改了行new.k = v
,setattr(new,k,v)
它起作用了。不知道有什么区别...