且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Django 1.5 / 1.6中设置两种不同类型的用户

更新时间:2022-04-19 05:19:26

似乎有一些常见的功能和不常见的功能给您的用户类型。如果您的用户类型中存在Django的默认用户模型不支持开箱即用的常见功能,则应直接将其子类化。

It seems like there are some common features and uncommon features to your user types. If there are common features in your user types that Django's default User model doesn't support out of the box, you should subclass it directly.

添加额外的,不常见的功能对于您的用户类型***不要通过子类化,而是使用配置文件。我的理由是因为您对这些用户类型的身份验证不会从根本上改变,但用户的详细信息取决于用户的类型。为了适应这一点,您可以创建一个单独的模型与这些细节,并将您的User类作为OneToOne / ForeignKey关系(取决于您的设计)引用。

Adding in extra, uncommon features to your user types are best done not by subclassing but by using a profile. My rationale for this is because your authentication for these user types doesn't fundamentally change, but details about the user does depending on the type of user it is. To accomodate this, you create a separate model with these details and reference your User class as a OneToOne/ForeignKey relationship (depending on your design).

您可以修改您的用户创建过程,以确定应该是什么类型的用户类型,并将其关联的OneToOneField / ForeignKey(取决于您的设计)设置为适当的客户类型模型。

You can make modifications to your user creation process to identify what kind of user type it should be, and set its associated OneToOneField/ForeignKey (depending on your design) to the appropriate customer type model.

通过这样做,您应该只有一个AUTH_USER_MODEL,您应该可以处理不同客户类型的详细信息。

By doing it this way, you should only have one AUTH_USER_MODEL, and you should be able to handle details for your different customer types.