我试图扩展Group
的{{1}}和Permission
模型,以获得一些额外的功能。这是我到目前为止所做的:
django.contrib.auth.models
我试图覆盖from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin, ContentType, Permission as Auth_Permission, Group as Auth_Group
class Group(Auth_Group):
# Inherited attributes from django.contrib.auth.Group
# name = models.Charfield()
code = models.CharField(max_length=40, unique=True)
group = models.OneToOneField(Auth_Group,
on_delete=models.CASCADE,
parent_link=True,
related_name="group_extra"
)
description = models.CharField(max_length=400, null=True, blank=True)
def __unicode__(self):
return self.code
class Account(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
username = models.CharField(max_length=40, unique=True)
first_name = models.CharField(max_length=40)
last_name = models.CharField(max_length=40)
groups = models.ManyToManyField(
Group,
verbose_name=_('groups'),
blank=True,
help_text=_(
'The groups this user belongs to. A user will get all permissions '
'granted to each of their groups.'
),
related_name="user_set",
related_query_name="user",
)
模型中Group
的表示形式,因此当我调用PermissionsMixin
时,它会将对象作为我的{返回} {1}}类我然后可以调用first_group = <Account>.groups.all()[0]
(这似乎工作正常)。问题在于我试着打电话给Group
......这就是:
first_group.code
正如您所看到的,第一个方法被称为<Account>.get_all_permissions()
被抛出,但第二次,它按预期工作。这似乎是某种初始化问题,尽管我没有对模型进行任何明确的初始化。当压倒这种关系时,有什么细微差别吗? (由其他功能使用)。
先谢谢。