Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django管理页面中填充datefield?_Python_Django - Fatal编程技术网

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()
不是字段。为什么不在数据库或表单级别设置默认值?它没有提到日期字段