带有Django mixin的鸡肉和鸡蛋的噩梦

问题描述 投票:0回答:1

我正在将一个基于Django的大型应用程序从Django 1.7应用程序升级到Django 2.2,并且在与权限相关的mixin方面遇到了很多麻烦。

   class PrincipalRoleRelation(models.Model):

    """A role given to a principal (user or group). If a content object is
    given this is a local role, i.e. the principal has this role only for this
    content object. Otherwise it is a global role, i.e. the principal has
    this role generally.

    user
        A user instance. Either a user xor a group needs to be given.

    group
        A group instance. Either a user xor a group needs to be given.

    role
        The role which is given to the principal for content.

    content
        The content object which gets the local role (optional).
    """

       :::

    user         = models.ForeignKey(User,        verbose_name=_(u"User"),         blank=True, null=True, on_delete=models.SET_NULL)
    group        = models.ForeignKey(Group,       verbose_name=_(u"Group"),        blank=True, null=True, on_delete=models.SET_NULL)
    role         = models.ForeignKey(Role,        verbose_name=_(u"Role"),                                on_delete=models.CASCADE)

       :::

但是,这在应用程序初始化期间无法加载,因为用户,组和角色等也是正在加载并且“ populate()不能重新进入”的应用程序(所以Dango抱怨)

我试图通过修改上面的代码来解决此问题,以创建一种“骨架”类,该类不尝试引用任何其他应用,例如:

app_models_loaded = True

try:
    from django.contrib.auth import get_user_model
    User = get_user_model()
except:
    app_models_loaded = False

if app_models_loaded:

    from django.contrib.auth.models import Group

    user         = models.ForeignKey(User,        verbose_name=_(u"User"),         blank=True, null=True, on_delete=models.SET_NULL)
    group        = models.ForeignKey(Group,       verbose_name=_(u"Group"),        blank=True, null=True, on_delete=models.SET_NULL)
    role         = models.ForeignKey(Role,        verbose_name=_(u"Role"),                                on_delete=models.CASCADE)
              :::

然后在manage.py中,我定义了完整的mixin类,称为PrincipalRoleRelation2,并通过代码覆盖了骨架类:

from django.contrib import admin

from permissions.models import PrincipalRoleRelation

if admin.site.is_registered(PrincipalRoleRelation):
    admin.site.unregister(PrincipalRoleRelation)

admin.site.register(PrincipalRoleRelation, PrincipalRoleRelation2)

但是,尽管这似乎几乎可行,但是我没有看到某些PrincipalRoleRelation2属性,例如“ role”,我希望是重新映射的PrincipalRoleRelation类,其中存在所有属性。

我觉得我正在把自己挖进一个越来越深的洞,并且上述方法是不正确的,并且永远无法正常工作。因此,任何帮助将不胜感激!

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-== -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-===>

编辑:为响应schillingt的评论,User类的定义如下:

class User(AbstractBaseUser):  # , PermissionsMixin):
    """ Custom user model
        Currently just used by the tests for django-permissions

        All unique user fields required for a user
        NB: Fields that are customizable across multiple identities will be part of a Profile object
    """
    # Dont use PermissionsMixin since not using contrib.auth.models.Permissions
    # and not using authentication backend perms ... so its only relevant for groups
    # ... however it causes user.groups relations name clashes ..
    # But we are using the groups part with django-permissions:
    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 '
                                                            'his/her group.'),
                                    related_name="user_set", related_query_name="user")
    is_superuser = models.BooleanField(_('superuser status'), default=False,
                                       help_text=_('Designates that this user has all permissions without '
                                                   'explicitly assigning them.'))

    username = models.EmailField(_('Email (Username)'), max_length=255, unique=True)
    # Make username an email and just dummy in email here so its clearer for user.email use cases

我正在将一个基于Django的大型应用程序从Django 1.7应用程序升级到Django 2.2,并且在与权限相关的mixin方面遇到了很多麻烦。类PrincipalRoleRelation(models.Model):“”“角色...

python django mixins
1个回答
0
投票

作为循环引用的一种解决方案,django可以指定对相关模型具有字符串引用的ForeignKey(或任何其他关系字段),而无需导入实际的类。

© www.soinside.com 2019 - 2024. All rights reserved.