Django admin : show records for respective user only

Viewed 5347

I have a simple model, say Resources. And I have fewer than 20 users and model admin serves the purpose to record requests.

Problem is that all users can see all records in model admin site.

Can this behaviour be changed to only show records created by same user only ?

Thank you in anticipation.

2 Answers

UPDATE 2020: Anyone who is curious as to what is the author field, then that is established in the models.py file of your app. For the admin.py part, you can visit the docs.

Step 1:

Make sure in your permissions you give access to the apps you want your users to have CRUD functionality over.

Step 2:

Admin.py

class MyModelAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(author=request.user)

Models.py

from django.contrib.auth import get_user_model

class Lecture(models.Model):
    author = models.ForeignKey(get_user_model(), null=True, on_delete=models.CASCADE)
Related