嘗試了以下內容:如何在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