Created
November 27, 2024 01:36
-
-
Save douglasmiranda/92761ca81fa31107c3d27ec3ac0087e3 to your computer and use it in GitHub Desktop.
Verify if the current Django Admin view is changelist.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# There are many ways to do it, like checking if "change" is in the current url path | |
# you can adapt to check for _changelist, _change, _delete... | |
def is_changelist_view(model_admin_instance, request): | |
""" | |
Verify if the current view is changelist. | |
Args: | |
model_admin_instance (admin.ModelAdmin): ModelAdmin instance. | |
request (HttpRequest): Current request. | |
Examples: | |
@admin.register(Article) | |
class ArticleAdmin(admin.ModelAdmin): | |
def get_queryset(self, request): | |
if is_changelist_view(self, request): | |
return queryset.only("title") | |
""" | |
opts = model_admin_instance.opts | |
url_name = request.resolver_match.url_name | |
return url_name == f"{opts.app_label}_{opts.model_name}_changelist" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment