diff --git a/django/contrib/auth/backends.py b/django/contrib/auth/backends.py
index db99c94..d045d6a 100644
a
|
b
|
|
1 | 1 | from __future__ import unicode_literals |
2 | 2 | from django.contrib.auth import get_user_model |
3 | 3 | from django.contrib.auth.models import Permission |
| 4 | from django.db.models.fields import FieldDoesNotExist |
4 | 5 | |
5 | 6 | |
6 | 7 | class ModelBackend(object): |
… |
… |
class ModelBackend(object):
|
30 | 31 | if user_obj.is_superuser: |
31 | 32 | perms = Permission.objects.all() |
32 | 33 | else: |
33 | | user_groups_field = get_user_model()._meta.get_field('groups') |
34 | | user_groups_query = 'group__%s' % user_groups_field.related_query_name() |
35 | | perms = Permission.objects.filter(**{user_groups_query: user_obj}) |
| 34 | try: |
| 35 | user_groups_field = get_user_model()._meta.get_field('groups') |
| 36 | user_groups_query = 'group__%s' % user_groups_field.related_query_name() |
| 37 | perms = Permission.objects.filter(**{user_groups_query: user_obj}) |
| 38 | except FieldDoesNotExist: |
| 39 | # custom user object may not define groups |
| 40 | perms = Permission.objects.none() |
36 | 41 | perms = perms.values_list('content_type__app_label', 'codename').order_by() |
37 | 42 | user_obj._group_perm_cache = set(["%s.%s" % (ct, name) for ct, name in perms]) |
38 | 43 | return user_obj._group_perm_cache |
… |
… |
class ModelBackend(object):
|
41 | 46 | if user_obj.is_anonymous() or obj is not None: |
42 | 47 | return set() |
43 | 48 | if not hasattr(user_obj, '_perm_cache'): |
44 | | user_obj._perm_cache = set(["%s.%s" % (p.content_type.app_label, p.codename) for p in user_obj.user_permissions.select_related()]) |
| 49 | if hasattr(user_obj, 'user_permissions'): |
| 50 | user_obj._perm_cache = set(["%s.%s" % (p.content_type.app_label, p.codename) for p in user_obj.user_permissions.select_related()]) |
| 51 | else: |
| 52 | user_obj._perm_cache = set() |
45 | 53 | user_obj._perm_cache.update(self.get_group_permissions(user_obj)) |
46 | 54 | return user_obj._perm_cache |
47 | 55 | |