0

I am building a Django app which can be used by multiple companies. Now I have set up the common(super) admin, which can look every users and model. I am storing all the company details on the company table.

Suppose we've 3 companies - A, B and C. I want to create 3 sub-admins for each company, where they can only see/edit all the data related to their company users only.

What I've tried already: I created permissions based on the models but they're not solving the purpose of accessing data from the only single company.

Can anyone explain to me how this can be achieved? Either programmatically or from the admin portal.

Tech Specs:

  • Django 2.1

  • Python 3.6.4

PS: If you need more information, then please let me know. I will provide as much information as I can. Thank you.

disp_name
  • 1,448
  • 2
  • 20
  • 46
  • 1
    You can do this by overriding the `queryset` method on all of your `ModelAdmin` classes. But keep in mind that the Django Admin was not really meant to be used by end-users. – voodoo-burger Feb 25 '19 at 11:28
  • Possible duplicate of [Override default queryset in Django admin](https://stackoverflow.com/questions/12354099/override-default-queryset-in-django-admin) – voodoo-burger Feb 25 '19 at 11:28
  • Yes, not all end users will be using Admin... But Company A will have 1 admin responsible for company A users, same for all other companies. – disp_name Feb 25 '19 at 11:59
  • Also, I couldn't understand much from the URL provided, if you could give some more explanation for my scenario it would be much helpful! – disp_name Feb 25 '19 at 12:00
  • From experience, it's usually simpler to write a distinct app for those use cases - as voodoo-burger mentions, the admin is really an internal admin tool, and it's crud-based architecture seldom matches business domain needs and workflows. – bruno desthuilliers Feb 25 '19 at 15:39

1 Answers1

1

If you are determined to use the Django Admin you can override each ModelAdmin's get_queryset() function to only show records related to the currently logged in user's company. For example:

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

Note that this depends on your user having a company attribute, and that you will have to apply this to every single instance of your ModelAdmin classes.

This is also described in the official documentation: https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_queryset .

voodoo-burger
  • 2,123
  • 3
  • 22
  • 29
  • Will it override the existing admin user or create a new type of user? Because I want one admin which works exactly like the way now. But more like moderators for each company where they can access only their data and not others. – disp_name Feb 26 '19 at 12:40
  • 1
    @disp_name I don't think you quite understand how the admin and users work.. you should try to get started and then come back and ask specific questions when you get stuck. – voodoo-burger Feb 26 '19 at 13:17
  • Sure, I will look into it and revert. Thank you so much for help @voodoo-burger – disp_name Feb 27 '19 at 14:04