Python 如何在django管理页面中填充datefield?
尝试了以下操作:Python 如何在django管理页面中填充datefield?,python,django,Python,Django,尝试了以下操作: 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_fiel
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.
:(admin.E028)“预填充字段”的值指的是“发布日期”,它不能是DateTimeField、ForeignKey或ManyToManyField。
:(admin.E029)“预填充字段[“发布日期”]”的值必须是列表或元组。
您忘记了元组的逗号
from datetime import datetime
class ArticleAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('title',), 'pub_date': (datetime.now(), ) }
预填充的_字段不接受DateTimeField、ForeignKey或ManyToManyField字段
正如文档()中所述,
预填充的_字段将字段映射到它们将从中填充的其他字段datetime.now()
不是字段。为什么不在数据库或表单级别设置默认值?它没有提到日期字段