1

I need to build one instance of django admin that can be used by multiple companies. the schema is :

Company1
     Branch 1
          User 1
          User 2
     Branch 2
          User 3
          .......

Company 2
     Branch 1
          User 4
          User 5
     Branch 2
          User 6
          .......

The idea behind this is that user 1 and 2 is able to see (but cannot edit ) user's 3 stuff. Where as users 1 and 2 can see and edit each other's content. All this within the Company1 scope (only).

My question is are there any devs out there who's faced a similar problem and want to share their thoughts on how this can be achieved in dj admin? Any additional packages which can be utilized to extend dj admin functionality in right direction ?

Im aware that this challenges the idea of what was dj admin designed for (no need to caution about this ) ... but since there isn't enough hands to design and build something from a scratch for this project i need to tap into dj admin functionality as much as i can.

All thoughts will greatly be appreciated !

zzart
  • 11,207
  • 5
  • 52
  • 47

1 Answers1

1

I was in a similar situation, with the added requirement that a user may be in multiple companies, and can "switch". For that purpose I put the "current company" into a session. If in your case you'd just be looking up in the user what they're allowed to see, it should be very easy, by overriding ModelAdmin.queryset, for example:

class CompanyGogglesAdmin(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(CompanyGoggleAdmin, self).queryset(request)
        user_company = request.user.company
        return qs.filter(**{ 'company' : user_company })

You could use CompanyGogglesAdmin as a base class for all those models that can be filtered by "company" which gets looked up by the user's company. You could also make that company field configurable, or - like I did - look up the "current" company from a session rather than the user. See also How can I implement a global, implicit filter in Django admin?

Community
  • 1
  • 1
Danny W. Adair
  • 12,498
  • 4
  • 43
  • 49
  • Thanks for your time Danny! I v much like the idea of a base class. Having had a little think about dj-admin approach i think the amount of hair i will pull out trying to make admin do what i need, is significant :) So painful as it is i will reimplement CRUD once again , get bored and keep my hair. – zzart Oct 20 '11 at 09:59