Multiprofile与抽象模型

时间:2016-05-17 12:56:23

标签: python django abstract-class user-profile

我需要两种类型的配置文件:

1)第一个通过onetoone关系链接到Users,用一些新字段扩展它。

2)与用户无关的秒(并且只能由超级用户/管理员访问):它们只是对象。

问题在于我想要一种简单的方法将配置文件从1°类型更改为第二种类型,反之亦然。

在“Django Designer Patterns”中,我找到了一个这样的例子:

class BaseProfile(models.Model):
    User_Types = (
        (0, 'active'),
        (1, 'inactive'),
    )
    user_type = models.IntegerField(null=True, choices=User_Types, default=0)
    name = models.CharField(max_length=32, unique=True)
    #many other common field

    class Meta:
        abstract = True

    def __str__(self):
        return self.name

class Profile_Active(models.Model):
    user = models.OneToOneField(User, blank=True, on_delete=models.PROTECT, 
        related_name='profile_active', primary_key=True) #this was in BaseProfile
    #some more field reserved for this type of profile

    class Meta:
        abstract = True


class Profile_Inactive(models.Model):
    #some field reserved for this type of profile

    class Meta:
        abstract = True


class Profile(Profile_Inactive, Profile_Active, BaseProfile):
    pass

但我不明白我应该如何将Profile中的抽象模型组合起来(我应该写什么而不是“传递”?)以及我应该实现的目标? Profile 将拥有三个模型的所有领域?哪个获得方面使用单一型号?

也许我可以忘记Profile并使用Profile_Inactive(BaseProfile)Profile_Active(BaseProfile)?但那么如何从一个变为另一个?

我很困惑,请帮忙

0 个答案:

没有答案