Hide all records in Django admin unless a search query is entered

532 views Asked by At

We have a Python-2.7 and Django project (version 1.11) that has lots of models registered on it (Users app, Invitations app, Profiles app, etc...)

First thing I'm trying to do is create a group for some users that will have only "View" permissions when they log on to the Django admin and see limited models from the entire list. That said, these users should only be able to browse through records in models I specify for their group.

Second thing is to not have any records shown by default when you enter one of the models you're allowed to view. You must search for something in order for matching records to appear so that only results associated with that user is shown. Is this possible and if yes, how do I implement this?

1

There are 1 answers

2
user2390182 On

Override get_queryset of the model admin and check for query params:

from django.contrib import admin

@admin.register(MyModel)
class CustomAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if not self.get_preserved_filters(request):
            return qs.none()
        return qs