Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20244: PermissionsMixin now defines a related_query_name for M2Ms

  • Loading branch information...
commit f325f86971bf7fc30e3daafc9835f66cd7951e3a 1 parent 12cb0df
Andrew Godwin authored June 27, 2013
6  django/contrib/auth/models.py
@@ -295,10 +295,12 @@ class PermissionsMixin(models.Model):
295 295
     groups = models.ManyToManyField(Group, verbose_name=_('groups'),
296 296
         blank=True, help_text=_('The groups this user belongs to. A user will '
297 297
                                 'get all permissions granted to each of '
298  
-                                'his/her group.'))
  298
+                                'his/her group.'),
  299
+        related_name="user_set", related_query_name="user")
299 300
     user_permissions = models.ManyToManyField(Permission,
300 301
         verbose_name=_('user permissions'), blank=True,
301  
-        help_text='Specific permissions for this user.')
  302
+        help_text='Specific permissions for this user.',
  303
+        related_name="user_set", related_query_name="user")
302 304
 
303 305
     class Meta:
304 306
         abstract = True
22  django/contrib/auth/tests/test_custom_user.py
@@ -4,7 +4,9 @@
4 4
     AbstractBaseUser,
5 5
     AbstractUser,
6 6
     UserManager,
7  
-    PermissionsMixin
  7
+    PermissionsMixin,
  8
+    Group,
  9
+    Permission,
8 10
 )
9 11
 
10 12
 
@@ -81,6 +83,20 @@ def is_staff(self):
81 83
         return self.is_admin
82 84
 
83 85
 
  86
+# At this point, temporarily remove the groups and user_permissions M2M
  87
+# fields from the AbstractUser class, so they don't clash with the related_name
  88
+# that sets.
  89
+
  90
+old_au_local_m2m = AbstractUser._meta.local_many_to_many
  91
+old_pm_local_m2m = PermissionsMixin._meta.local_many_to_many
  92
+groups = models.ManyToManyField(Group, blank=True)
  93
+groups.contribute_to_class(PermissionsMixin, "groups")
  94
+user_permissions = models.ManyToManyField(Permission, blank=True)
  95
+user_permissions.contribute_to_class(PermissionsMixin, "user_permissions")
  96
+PermissionsMixin._meta.local_many_to_many = [groups, user_permissions]
  97
+AbstractUser._meta.local_many_to_many = [groups, user_permissions]
  98
+
  99
+
84 100
 # The extension user is a simple extension of the built-in user class,
85 101
 # adding a required date_of_birth field. This allows us to check for
86 102
 # any hard references to the name "User" in forms/handlers etc.
@@ -178,3 +194,7 @@ class CustomUserBadRequiredFields(AbstractBaseUser):
178 194
 
179 195
     class Meta:
180 196
         app_label = 'auth'
  197
+
  198
+# Undo swap hack
  199
+AbstractUser._meta.local_many_to_many = old_au_local_m2m
  200
+PermissionsMixin._meta.local_many_to_many = old_pm_local_m2m

0 notes on commit f325f86

Please sign in to comment.
Something went wrong with that request. Please try again.