2

Tried the following:

from datetime import datetime
class ArticleAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',), 'pub_date':(datetime.now()) }

Which raises:

ERRORS:

<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.
1
  • As it says in the docs (docs.djangoproject.com/en/1.8/ref/contrib/admin/…), prepopulated_fields maps fields to other fields they are going to be populated from. datetime.now() is not a field. Why don't you set the default value on either database or form level?
    – kaveh
    Commented Jun 9, 2015 at 3:15

2 Answers 2

1

You were forgetting the comma for tuple.

from datetime import datetime
class ArticleAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',), 'pub_date': (datetime.now(), ) }
0
1

prepopulated_fields doesn’t accept DateTimeField, ForeignKey, nor ManyToManyField fields.

From the docs.

1
  • It doesn't mention DateField Commented Mar 19, 2019 at 10:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.