Django - 多个用户配置文件 [英] Django - Multiple User Profiles
问题描述
最初,我开始使用UserProfile:
Initially, I started my UserProfile like this:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
verified = models.BooleanField()
mobile = models.CharField(max_length=32)
def __unicode__(self):
return self.user.email
与 AUTH_PROFILE_MODULE ='accounts.UserProfile'
中的 settings.py
。
然而,我的网站中有两种不同类型的用户,每个用户都有自己独特的属性。例如,我希望我的个人用户只有一个用户,因此具有 user = models.OneToOneField(User)
,对于企业我想要他们有多个用户相同的配置文件,所以我将 user = models.ForeignKey(User)
。
However, I have two different kinds of users in my website, Individuals and Corporate, each having their own unique attributes. For instance, I would want my Individual users to have a single user only, hence having user = models.OneToOneField(User)
, and for Corporate I would want them to have multiple users related to the same profile, so I would have user = models.ForeignKey(User)
instead.
所以我考虑将模型分为两个不同的模型: IndivProfile
和 CorpProfile
,都继承自 UserProfile
,同时将特定于模型的属性移动到相关子模型中。似乎是一个好主意,可能会工作,但是我不能指定 AUTH_PROFILE_MODULE
这样,因为我有两个不同的用户配置文件
So I thought about segregating the model into two different models, IndivProfile
and CorpProfile
, both inheriting from UserProfile
while moving the model-specific attributes into the relevant sub-models. Seems like a good idea to me and would probably work, however I would not be able to specify AUTH_PROFILE_MODULE
this way since I'm having two user profiles that would be different for different users.
我还想到了另外一种方式,让 UserProfile
继承自多个类(模型) ,如下所示:
I also thought about doing it the other way around, having UserProfile
inherit from multiple classes (models), something like this:
class UserProfile(IndivProfile, CorpProfile):
# some field
def __unicode__(self):
return self.user.email
这样我将设置 AUTH_PROFILE_MODULE ='accounts.UserProfile'
并解决其问题。但是,这并不像它的工作原理,因为python的继承从左到右是有用的,而 IndivProfile
中的所有变量将占主导地位。
This way I would set AUTH_PROFILE_MODULE = 'accounts.UserProfile'
and solve its problem. But that doesn't look like it's going to work, since inheritance in python works from left to right and all the variables in IndivProfile
will be dominant.
当然,我可以随时使用 IndivProfile
和 CorpProfile
在一起,然后我会在必要的时候使用所需的。但是这只是看起来不干净,我宁愿把它们分开,并在适当的地方使用适当的模型。
Sure I can always have one single model with IndivProfile
and CorpProfile
variables all mixed in together and then I would use the required ones where necessary. But that is just doesn't look clean to me, I would rather have them segregated and use the appropriate model in the appropriate place.
任何一个
Any suggestions of a clean way of doing this?
推荐答案
我这样做了。
PROFILE_TYPES = (
(u'INDV', 'Individual'),
(u'CORP', 'Corporate'),
)
# used just to define the relation between User and Profile
class UserProfile(models.Model):
user = models.ForeignKey(User)
profile = models.ForeignKey('Profile')
type = models.CharField(choices=PROFILE_TYPES, max_length=16)
# common fields reside here
class Profile(models.Model):
verified = models.BooleanField(default=False)
使用中间表来反映两个抽象模型之间的关系, User
,它已经被定义为n Django和我的个人资料
模型。如果属性不常见,我将创建一个新模型,并将其与 Profile
。
I ended up using an intermediate table to reflect the relation between two abstract models, User
which is already defined in Django, and my Profile
model. In case of having attributes that are not common, I will create a new model and relate it to Profile
.
这篇关于Django - 多个用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!