Fix guardian import (#4612)

This commit is contained in:
Tom Christie 2016-10-21 16:59:43 +01:00 committed by GitHub
parent 0fe0e1e703
commit 30bf9df5d0
2 changed files with 6 additions and 2 deletions

View File

@ -207,7 +207,6 @@ guardian = None
try: try:
if 'guardian' in settings.INSTALLED_APPS: if 'guardian' in settings.INSTALLED_APPS:
import guardian import guardian
import guardian.shortcuts # Fixes #1624
except ImportError: except ImportError:
pass pass

View File

@ -289,6 +289,11 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend):
perm_format = '%(app_label)s.view_%(model_name)s' perm_format = '%(app_label)s.view_%(model_name)s'
def filter_queryset(self, request, queryset, view): def filter_queryset(self, request, queryset, view):
# We want to defer this import until run-time, rather than import-time.
# See https://github.com/tomchristie/django-rest-framework/issues/4608
# (Also see #1624 for why we need to make this import explicitly)
from guardian.shortcuts import get_objects_for_user
extra = {} extra = {}
user = request.user user = request.user
model_cls = queryset.model model_cls = queryset.model
@ -302,4 +307,4 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend):
extra = {'accept_global_perms': False} extra = {'accept_global_perms': False}
else: else:
extra = {} extra = {}
return guardian.shortcuts.get_objects_for_user(user, permission, queryset, **extra) return get_objects_for_user(user, permission, queryset, **extra)