Opened 12 months ago
Closed 12 months ago
#35089 closed New feature (invalid)
Allow admin filtering on GenericForeignKey and GenericRelation relations via search_fields
Reported by: | Chase Adams | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 5.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This ticket is to enable normal Admin search_fields syntax for GenericForeignKey and GenericRelation fields including additional query expression relation traversal using double underscore syntax.
For instance given these models:
class ModelA(Model): title = models.CharField(max_length=100) content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type', 'object_id') class ModelB(Model): name = models.CharField(max_length=100) related_as = GenericRelation(ModelA)
Enable admin search in this format:
class ModelAAdmin(ModelAdmin): search_fields = ['content_object__name'] admin.site.register(ModelA, ModelAAdmin) class ModelBAdmin(admin.ModelAdmin): search_fields = ['related_as__title'] admin.site.register(ModelB, ModelAAdmin)
Additionally, further double underscore relation traversal should be implemented:
search_fields = ['content_object__foreignkey_field__name']
search_fields = ['related_as__other_content_object__name']
Note:
See TracTickets
for help on using tickets.