我使用Django 1.9,并有模型例如为:如何在模型表单中使用父母模型字段?
class TimeStampedModel(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
后,我用这个模型来添加created
和modified
到我的模特一些继承。但我不能将这些字段用于表单。例如: -
class Customer(TimeStampedModel):
first_name = models.CharField(max_length=250, blank=True,
null=True, default=None)
last_name = models.CharField(max_length=250, blank=True,
null=True, default=None)
和表单代码:
class CustomerForm(forms.ModelForm):
class Meta:
model = Customer
fields = ('first_name', 'last_name', 'modified')
readonly_fields = ('modified',)
我得到一个错误:
django.core.exceptions.FieldError: Unknown field(s) (modified) specified for ...
我怎么能这个字段添加到窗体?我在数据库模式中,但Django的形式并没有得到它。
你的意思是没有办法表现出来?我不想改变它,但显示。 –
auto_now = True字段在表单中不可用。因为表单是用于编辑值的。所以做一件事情,创建一个html输入,并通过current_time''。这将显示您的模板中的当前时间。 –