A lot of my models have a foreign key to a "Company" model. Every logged in user can be part of one or more companies (User m2m Company, not null).
I would like the current admin user to have "Company goggles" on, i.e. a select list, on the admin index page or maybe the base header, where they can switch their "current" Company. Doing that should automatically apply a "company equals" filter - for models that have a foreign key to Company - in addition to any other filters.
What's the best way to achieve this?
NB: This is meant as a comfort function for the admin interface, actual protection of models is not necessary at this stage (client views do need that but I can just use a custom Manager and lookup via request.user there).
My current idea is:
Store current company in session.
Use middleware to look up current company from session, and append the company to all relevant links:
a) change_list: (?/&)"company__eq=42"
b) change_view "add?company=42" for models that have a foreign key to Company.
This may require to reverse or pattern match the URLs to find out their model and check it for presence of the foreign key (or I might prepare that list beforehand to improve performance).
Include in each ModelAdmin form the foreign key field, but hide it via CSS, so that change_view add ("new") includes the preset foreign key value from the link without mentioning it.
Do you find this a viable approach?
If http://code.djangoproject.com/ticket/10761 was implemented I guess I could just specify a custom queryset which reads the current company from request.session and be done with it. Maybe better to fast-track (=make and submit patch) that ticket instead?
EDIT: or maybe just redefine the queryset() method on every ModelAdmin that needs it / has the foreign key?
My vote is is for overriding ModelAdmin.queryset
, since you conveniently have access to the request there. Override save_model
for point 3.
class MyModelAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(MyModelAdmin, self).queryset(request)
if request.session.get('company_goggles'):
return qs.filter(company=request.session['company_goggles'])
return qs
If you have many models, I'd subclass ModelAdmin
as something like GogglesAdmin
and define a field / default to pull the fieldname from and also the pre-save auto injecting of company.
class CompanyGogglesAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(CompanyGoggleAdmin, self).queryset(request)
if request.session.get('company_goggles'):
return qs.filter(**{ getattr(self, 'company_field', 'company') :
request.session['company_goggles'] })
By the way, I really like this "company goggles" terminology.
To answer the last question: If you just want to display certain items of your queryset you can override the ModelAdmin
's queryset()
method withoud problems. For example, if you'd set a company in the current session. You can furthermore overwrite the save_model()
method to have the company
ForeignKey
always point to the user's company when saving the form:
class MyAdmin(admin.ModelAdmin):
def queryset(self, request):
company = request.session.get('company', None)
qs = self.model._default_manager.get_query_set()
if not company is None:
qs = qs.filter(company=company)
return qs
def save_model(self, request, obj, form, change):
company = request.session.get('company', None):
instance = form.save(commit=False)
if not change or not instance.company:
instance.company = company
instance.save()
form.save_m2m()
return instance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With