Fixed #20244: PermissionsMixin now defines a related_query_name for M2Ms

This commit is contained in:
Andrew Godwin 2013-06-27 15:40:26 +01:00
parent 12cb0df10f
commit f325f86971
2 changed files with 25 additions and 3 deletions

View File

@ -295,10 +295,12 @@ class PermissionsMixin(models.Model):
groups = models.ManyToManyField(Group, verbose_name=_('groups'), groups = models.ManyToManyField(Group, verbose_name=_('groups'),
blank=True, help_text=_('The groups this user belongs to. A user will ' blank=True, help_text=_('The groups this user belongs to. A user will '
'get all permissions granted to each of ' 'get all permissions granted to each of '
'his/her group.')) 'his/her group.'),
related_name="user_set", related_query_name="user")
user_permissions = models.ManyToManyField(Permission, user_permissions = models.ManyToManyField(Permission,
verbose_name=_('user permissions'), blank=True, verbose_name=_('user permissions'), blank=True,
help_text='Specific permissions for this user.') help_text='Specific permissions for this user.',
related_name="user_set", related_query_name="user")
class Meta: class Meta:
abstract = True abstract = True

View File

@ -4,7 +4,9 @@ from django.contrib.auth.models import (
AbstractBaseUser, AbstractBaseUser,
AbstractUser, AbstractUser,
UserManager, UserManager,
PermissionsMixin PermissionsMixin,
Group,
Permission,
) )
@ -81,6 +83,20 @@ class CustomUser(AbstractBaseUser):
return self.is_admin return self.is_admin
# At this point, temporarily remove the groups and user_permissions M2M
# fields from the AbstractUser class, so they don't clash with the related_name
# that sets.
old_au_local_m2m = AbstractUser._meta.local_many_to_many
old_pm_local_m2m = PermissionsMixin._meta.local_many_to_many
groups = models.ManyToManyField(Group, blank=True)
groups.contribute_to_class(PermissionsMixin, "groups")
user_permissions = models.ManyToManyField(Permission, blank=True)
user_permissions.contribute_to_class(PermissionsMixin, "user_permissions")
PermissionsMixin._meta.local_many_to_many = [groups, user_permissions]
AbstractUser._meta.local_many_to_many = [groups, user_permissions]
# The extension user is a simple extension of the built-in user class, # The extension user is a simple extension of the built-in user class,
# adding a required date_of_birth field. This allows us to check for # adding a required date_of_birth field. This allows us to check for
# any hard references to the name "User" in forms/handlers etc. # any hard references to the name "User" in forms/handlers etc.
@ -178,3 +194,7 @@ class CustomUserBadRequiredFields(AbstractBaseUser):
class Meta: class Meta:
app_label = 'auth' app_label = 'auth'
# Undo swap hack
AbstractUser._meta.local_many_to_many = old_au_local_m2m
PermissionsMixin._meta.local_many_to_many = old_pm_local_m2m