Open JackAtOmenApps opened 4 years ago
Since we don't use django admin on the projects we built this for, we never tested it or prepared for it. I assume the easiest way is to build a middleware that disables scopes entirely for requests to admin. If you want to go down this route, feel free to add a note to the documentation!
I think it is not easy to determine in middleware if a request is for the admin interface. (there can be more than one.)
So i wrote a class for ModelAdmin, which change the get_queryset like this:
from django_scopes import scopes_disabled
class DisabledScopesAdminMixin:
def get_queryset(self, request):
with scopes_disabled():
return super().get_queryset(request)
Thanks for the snippet @limugob.
I have an odd scenario where it works on the listing but not the change aspect of the admin model. Any ideas? Is there another method of the AdminModel that may need to have its scope disabled?
I found another approach inspired by some code I found in pretix:
from django.http import HttpRequest
from django_scopes import scopes_disabled
from django.urls import get_script_prefix
def ignore_scopes_in_admin_middleware(get_response, admin_path: str="admin"):
def middleware(request: HttpRequest):
if request.path.startswith(get_script_prefix() + admin_path):
with scopes_disabled():
return get_response(request)
return get_response(request)
return middleware
@limugob's solution probably does not work since scopes are disabled while the queryset is created, but not when it is lazily executed. @cboden's approach should work reliably (disables scoping for the admin entirely). I would make sure that your startswith
argument ends with a /
though, otherwise an endpoint like /adminimization
would also be exempt from scoping.
Sorry my approach worked only for the overview of objects, not for editing. I changed it to https://github.com/limugob/big_eggs/blob/master/big_eggs/tenants/middleware.py
There seems to be a problem with using this package along with django's admin. If I try to view the listing for a scoped model, I get:
"Database error Something's wrong with your database installation. Make sure the appropriate database tables have been created, and make sure the database is readable by the appropriate user."
If I click "+" to add a scoped model instance, I get the following error and traceback:
django_scopes.exceptions.ScopeError django_scopes.exceptions.ScopeError: A scope on dimension(s) site needs to be active for this query.
I don't see any admin-specific notes in the documentation (except creating commands), and I don't see a way around this.
Surely I'm missing something simple. Any recommendations?