尝试了以下内容:如何在django管理页面中填充datefield?
from datetime import datetime
class ArticleAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('title',), 'pub_date':(datetime.now()) }
这就提出:
错误:
<class 'news.admin.ArticleAdmin'>: (admin.E028) The value of 'prepopulated_fields' refers to 'pub_date', which must not be a DateTimeField, ForeignKey or ManyToManyField.
<class 'news.admin.ArticleAdmin'>: (admin.E029) The value of 'prepopulated_fields["pub_date"]' must be a list or tuple.
正如文档中所述(https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields),'prepopulated_fields'将字段映射到其他字段他们将被填充。 'datetime.now()'不是一个字段。为什么不在数据库或表单级设置默认值? – kaveh