Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32038 closed Bug (fixed)

Using EmptyFieldListFilter with GenericForeignKey and GenericRelation crashes.

Reported by: Javier Matos Odut Owned by: Mariusz Felisiak
Component: contrib.admin Version: 3.1
Severity: Release blocker Keywords: admin contenttypes
Cc: Federico Jaramillo Martínez Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a GenericForeignKey in my model:

content_type_resource_contract = models.ForeignKey(
    ContentType,
    on_delete=models.CASCADE,
    blank=True,
    null=True,
)
resource_contract_id = models.PositiveIntegerField(blank=True, null=True)
resource_contract = GenericForeignKey('content_type_resource', 'resource_contract_id')

and I want to use the new admin.EmptyFieldListFilter in my model admin:

class myAdmin(admin.ModelAdmin):
    list_filter = (('resource_contract', admin.EmptyFieldListFilter),)

But when I try to run it I get a "'GenericForeignKey' object has no attribute 'empty_strings_allowed'". It will work fine if I use the resource_contract_id field. Would it make sense to extend the GenericForeignKey to use those empty_strings_allowed attributes from fields that are used in this generic relation?

Change History (9)

comment:1 by Mariusz Felisiak, 4 years ago

Cc: Federico Jaramillo Martínez added
Owner: changed from nobody to Mariusz Felisiak
Severity: NormalRelease blocker
Status: newassigned
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Thanks for the report!

comment:2 by Mariusz Felisiak, 4 years ago

Summary: Cannot use EmptyFieldListFilter GenericForeignKey togetherUsing EmptyFieldListFilter with GenericForeignKey crashes.

comment:3 by Mariusz Felisiak, 4 years ago

Summary: Using EmptyFieldListFilter with GenericForeignKey crashes.Using EmptyFieldListFilter with GenericForeignKey and GenericRelation crashes.

After reconsideration I think a GenericForeignKey is out of scope for the original feature, see #28991. Moreover according to docs:

Due to the way GenericForeignKey is implemented, you cannot use such fields directly with filters (filter() and exclude(), for example) via the database API. Because a GenericForeignKey isn’t a normal field object, these examples will not work: ...

so I would add a note to EmptyFieldListFilter that it's not supported.


I've also found that it crashes with GenericRelation but we can fix it easily.

comment:4 by Mariusz Felisiak, 4 years ago

Has patch: set

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 33e91f3e:

Refs #32038 -- Added note to ModelAdmin.list_filter docs that GenericForeignKeys are not supported.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In e4ab44a:

Fixed #32038 -- Fixed EmptyFieldListFilter crash with GenericRelation.

Thanks Javier Matos Odut for the report.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 986cd28f:

[3.1.x] Refs #32038 -- Added note to ModelAdmin.list_filter docs that GenericForeignKeys are not supported.

Backport of 33e91f3ed8ce6412d9d0964acb59166579b9b20c from master

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In b7df7de4:

[3.1.x] Fixed #32038 -- Fixed EmptyFieldListFilter crash with GenericRelation.

Thanks Javier Matos Odut for the report.

Backport of e4ab44a4b2ef70be09c35c9197a19fd2e993b4d6 from master

comment:9 by Javier Matos Odut, 4 years ago

Thank you so much for fixing the issue!

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