因此,我有一个模式,我使用示范工厂。
如果我离开这种表格,我就没有错误:
class CheckinForm(ModelForm):
class Meta:
model = Checkout
fields = ( return_date , )
def __init__(self, *args, **kwargs):
super(CheckinForm, self).__init__(*args, **kwargs)
self.fields[ return_date ].widget = CheckboxInput()
如果我增列另一个领域,即extension
to the fields
,则我收到(1048,“Column book_id not bern”
,BUT,这些改动将节省到数据库。
我的模式如下:
class Checkout(models.Model):
book = models.ForeignKey(Book)
user = models.ForeignKey(User)
checkout_date = models.DateField(auto_now_add = True)
return_date = models.DateField(null = True, blank=True)
extension = models.IntegerField("Extension in days", blank = True, default = 0)
并在此处理形式:
def checkin(request):
c = RequestContext(request, dictionary)
CheckinFormSet = modelformset_factory(Checkout, CheckinForm)
if request.method == "POST":
data = request.POST.copy()
for i in range(0, int(data[ form-TOTAL_FORMS ])):
if form- + str(i) + -return_date in data:
data[ form- + str(i) + -return_date ] = datetime.date.today().isoformat()
else:
data[ form- + str(i) + -return_date ] =
formset = CheckinFormSet(data = data)
user_form = AutoUserForm(data = data)
if formset.is_valid():
c[ cool ] = cool
formset.save()
else:
c[ err ] = formset.errors
c[ data ] = data
else:
CheckinFormSet = modelformset_factory(Checkout, CheckinForm)
user_form = AutoUserForm()
c[ user_form ] = user_form
c[ form ] = CheckinFormSet
c[ context ] = checkin
return render_to_response( lib_admin/checkin.html , {}, c)
我不得不通过表格数据,因为对于<代码>return_date<>/code>的外地,我刚刚有一个用户将打上标的核对箱,而现在又加上这个日期。
This seems really weird to me, especially because the data is saved, even though I receive the IntegrityError.