假设我在 Python 中有一个模型类:
class Question(models.Model):
title = models.CharField('Title', max_length=300)
description = models.CharField('Description', max_length=800)
publish_date = models.DateTimeField('Pulished Date')
question_type = models.CharField(max_length=10)
我想将它与 ModelForm 绑定:
class QuestionForm(ModelForm):
class Meta:
model = Question
fields = ('title',
'description',
'publish_date',
'question_type')
当我尝试在 shell 中创建对象时,我收到此错误:
>>> form = QuestionForm()
>>> from django.utils import timezone
>>> form.publish_date = timezone.now().year
>>> form.save()
IntegrityError: (1048, "Column 'publish_date' cannot be null")
我在这里错过了什么吗?