#27304 closed Bug (invalid)
Django 1.10 onwards broke previous behaviour for models.DateTimeField() in Admin
Reported by: | Kegan Gan | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 1.10 |
Severity: | Normal | Keywords: | model admin datetime formfield_overrides AdminDateWidget widget |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In Django 1.9 and prior, we can use a date widget in Admin for a datetime
field. Specifically, we can override the default datetime
widget in Admin using AdminDateWidget
. In this way, user are only require to enter a date. The time will be default 0h 0m 0s.
From Django 1.10 onwards, this behaviour has break. Now, there is an error Enter a list of values.
on save in Admin.
Example code.
Model:
class Highlight(models.Model): end_at = models.DateTimeField() def save(self, *args, **kwargs): # Add 23 hours, 59 minutes, 59 seconds to end_at so that it represent end of day. extratime = timedelta(hours=23, minutes=59, seconds=59) if self.end_at: self.end_at = self.end_at + extratime super(Highlight, self).save(*args, **kwargs)
Admin:
class HighlightAdmin(admin.ModelAdmin): formfield_overrides = { models.DateTimeField: {'widget': widgets.AdminDateWidget}, }
Now there is no easy way to let user only input the date part of datetime
field in Admin.
Change History (3)
comment:1 by , 8 years ago
comment:2 by , 8 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Bisected to b9290b1d49538c1092b59c41e6046b11c25ecafb. To adapt your code, you must add 'form_class': forms.DateField
to the override.
comment:3 by , 8 years ago
Thanks Tim! It worked.
For reference, to override admin widget, use the following:
formfield_overrides = { models.DateTimeField: {'form_class': forms.DateTimeField, 'widget': widgets.AdminDateWidget}, }
Also refer to #26449.
Can you bisect to find where the behavior changed? Without looking into the details, I'm not immediately convinced we should treat this as a bug and provide backwards-compatibility for it, but we'll see.