| published by | Software Crafts |
|---|---|
| in blog | Software Crafts |
| original entry | Django Admin global filters |
This week I have been adding exports to a client Django admin for GDPR reasons. While doing this task it triggered a question or an idea, which was along the lines of:
I wonder if it's possible to filtered the whole admin by this single user? Are global filters a thing?
Having pondered it for a couple of days, this is probably where custom views would be more efficient way to do it since navigating the different database designs generically in a performantly is beyond my pay grade (or at least feels like it!)
Putting custom views aside and considering if it were to be done in the admin. I think having a filter or two be located in the header or opposite the breadcrumbs for the app and model. My thinking is that you would need to specify something globally, either register a global filter with the site object or subclass the admin site class. Next would be specifying how individual admins interact with that filter. For example a global date filter might be useful, but different models may want to intepret which date field(s) gets applied to the global filter.
Below is some possible pseudo-code of how the interface could look:
@admin.register_filter(User)
class UserFilter(admin.GlobalModelFilter):
filter_name = 'my_user_filter'
@admin.register(MyModel)
class MyModel(admin.ModelAdmin):
global_filter_fields = {
'my_user_filter': ['user', 'my_other_model__user']
}
Here the intention is to register a global filter, then the model admin is specifying the ORM filter LHS kwargs to use when interacting with that model admin. Hopefully that makes sense?
What do you think to this? Would this be a good addition to the admin? Obviously this would be best tested as a third-party package first.