Code

Opened 6 years ago

Closed 6 years ago

Last modified 4 months ago

#7993 closed Uncategorized (invalid)

Admin complains about a missing field even if it is not missing

Reported by: kratorius Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords: admin, improperlyconfigured
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Django raises an ImproperlyConfigured exception (NewsAdmin.fieldsets[1][1]['fields'] refers to field `pub_date` that is missing from the form) when the following admin interface is defined:

class NewsAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('title', 'text'),
        }),
        (_('Other stuff'), {
            'fields': ('language', 'pub_date'),
        }),
    )

But the model has a pub_date field:

class News(models.Model):
    title = models.CharField(max_length=50)
    pub_date = models.DateTimeField(auto_now=True, blank=True)
    text = models.TextField()
    language = models.ForeignKey(LanguageRef)

    def save(self):
        if not self.pub_date:
            self.pub_date = datetime.datetime.now()
        super(News, self).save()

    class Meta:
        get_latest_by = "pub_date"
        verbose_name = "news"
        verbose_name_plural = "news"

If I remove pub_date from the fields attribute in the NewsAdmin everything works as expected.

Attachments (0)

Change History (6)

comment:1 Changed 6 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

I suspect you get the error because auto_now=True is specified for this field. That makes it a non-editable field, since the value is not something the user can change, see http://www.djangoproject.com/documentation/model-api/#datefield:

"Note that the current date is always used; it’s not just a default value that you can override."

Since it's not editable there's no point in putting it in the form.

comment:2 follow-up: Changed 4 years ago by batiste

That would be still useful to be able to display the field in the admin. Maybe in a disabled input, ie: <input disabled />

comment:3 in reply to: ↑ 2 Changed 4 years ago by kmtracey

Replying to batiste:

That would be still useful to be able to display the field in the admin. Maybe in a disabled input, ie: <input disabled />

This is possible now (new in 1.2) with readonly fields in the admin, though it does not actually used disabled inputs.

comment:4 Changed 2 years ago by anonymous

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

For others who may run into this symptom for unrelated reasons, try restarting your server.

comment:5 follow-up: Changed 16 months ago by mwaigaryan@…

same problem in Django-1.4.2,is there a patch?

comment:6 in reply to: ↑ 5 Changed 4 months ago by alextreppass@…

Replying to mwaigaryan@…:

same problem in Django-1.4.2,is there a patch?

As above, put it in the readonly_fields list in the model admin, then it displays fine

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.