#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 , 5 years ago
| Cc: | added | 
|---|---|
| Owner: | changed from to | 
| Severity: | Normal → Release blocker | 
| Status: | new → assigned | 
| Triage Stage: | Unreviewed → Accepted | 
| Type: | Uncategorized → Bug | 
comment:2 by , 5 years ago
| Summary: | Cannot use EmptyFieldListFilter GenericForeignKey together → Using EmptyFieldListFilter with GenericForeignKey crashes. | 
|---|
comment:3 by , 5 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
GenericForeignKeyis implemented, you cannot use such fields directly with filters (filter()andexclude(), for example) via the database API. Because aGenericForeignKeyisn’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.
Thanks for the report!