Opened 8 weeks ago

Closed 6 weeks ago

Last modified 6 weeks ago

#35628 closed Bug (fixed)

date_hierarchy doesn't support GenerateField with output_field set to DateTimeField or DateField

Reported by: john-parton Owned by: john-parton
Component: contrib.admin Version: 5.0
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by john-parton)

If you have a GeneratedField with output_field set to DateField or DateTimeField, you cannot use it in the admin as a date_hierarchy because of a check. If you skip the check, it actually works just fine in my testing. I believe this the check could be adjusted to account for this.

# models.py
class TestModel(models.Model):
    date_range = DateTimeRangeField(_("Date range"))
    start_date = models.GeneratedField(
        expression=Lower("date_range"),
        output_field=models.DateTimeField(),
        db_persist=True,
    )
    
  

# admin.py
class TestAdmin(admin.ModelAdmin):
    date_hierarchy = "start_date"

    # Skipping checks actually makes this work exactly correctly
    def check(self, *args, **kwargs):
        return ()

I'll see if I can whip up a patch.

Change History (8)

comment:1 by john-parton, 8 weeks ago

Description: modified (diff)
Has patch: set
Needs documentation: set
Needs tests: set

comment:2 by john-parton, 8 weeks ago

Initial patch here: https://github.com/django/django/pull/18407

No docs or tests yet.

comment:3 by Simon Charette, 8 weeks ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:4 by Sarah Boyce, 8 weeks ago

Owner: set to john-parton
Status: newassigned

comment:5 by Claude Paroz, 6 weeks ago

Needs documentation: unset
Needs tests: unset
Triage Stage: AcceptedReady for checkin

Waiting for a final OK from Simon.

comment:6 by Sarah Boyce <42296566+sarahboyce@…>, 6 weeks ago

Resolution: fixed
Status: assignedclosed

In 7f8d8397:

Fixed #35628 -- Allowed compatible GeneratedFields for ModelAdmin.date_hierarchy.

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 6 weeks ago

In 88ebcb1:

[5.1.x] Fixed #35628 -- Allowed compatible GeneratedFields for ModelAdmin.date_hierarchy.

Backport of 7f8d839722b72aeb3ec5a4278ae57c18283acacd from main.

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 6 weeks ago

In c822ad6:

[5.0.x] Fixed #35628 -- Allowed compatible GeneratedFields for ModelAdmin.date_hierarchy.

Backport of 7f8d839722b72aeb3ec5a4278ae57c18283acacd from main.

Note: See TracTickets for help on using tickets.
Back to Top