在Django 1.5 / 1.6中设置两种不同类型的用户 [英] Setting up two different types of Users in Django 1.5/1.6

查看:115
本文介绍了在Django 1.5 / 1.6中设置两种不同类型的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这是,但是值得再次询问Django如何处理用户和身份验证的变化。

Please note--this is an updated version of my original question on this subject, but deserves to be asked again with the change in how Django deals with users and authentication.

我在两个不同类型的用户的网站上工作 - 让我们称之为客户店主。两者都在网站上注册,但功能截然不同。 客户只需一个配置文件,就可以在他们喜欢的商店之间购物。 商店所有者有一个帐户,但可以访问多个商店,每个商店可以有多个商店所有者

I'm working on a website with two very different kinds of users--let's call them Customers and Store Owners. Both register on the site, but have very different functionality. Customers simply have a single profile and can shop among the stores that they like. Store Owners have a single account but can have access to multiple stores, and each store can have multiple Store Owners.

模型的具体细节并不重要,但两种类型的用户将需要非常不同的字段。理想情况下,这些模型看起来像这样:

The exact details of the models don't matter, but the two types of users would require very different fields. The models ideally would look something like this:

Customer
  email (username)
  password
  name
  address
  time_zone
  preferred_shipping
  favorite_stores (many-to-many field)
  ...

Store Owner
  email (username)
  password
  name
  balance
  stores_owned (many-to-many field on Stores)
  stores_managed (many-to-many field on Stores)
  ...

最初,当Django的用户支持不佳时,我有一个 UserProfile 类,其中一些额外的字段在 OneToOne 用户然后额外的客户 StoreOwner OneToOne 用户配置。这没有很好的工作。

Originally, when Django had poor custom user support, I had a UserProfile class with some additional fields with a OneToOne on User, and then additional Customer and StoreOwner classes that were OneToOne on UserProfile. This didn't work very well.

鉴于Django 1.5 / 1.6中的变化,我试图想出最好的方法来构建它。现在,我有以下内容:

Given the changes in Django 1.5/1.6, I'm trying to come up with the best way to structure this. Right now, I have the following:

class CustomerUser(AbstractBaseUser):
    ...

class StoreOwnerUser(AbstractBaseUser):
    ...

但是因为会有两种类型的用户,我不能将 AUTH_USER_MODEL 设置为只有其中一个。

But because there would be two types of user, I can't set AUTH_USER_MODEL to only one of them.

什么是最好的方式来构建这个,以便我可以有两种不同类型的用户不同的领域,而不会导致我在用户身份验证,用户创建或管理员方面的任何问题?

另外,我如何能够从登录单独告诉这个用户是否是 CustomerUser StoreOwnerUser

Also, how will I be able to tell from login alone whether this user is a CustomerUser or a StoreOwnerUser?

推荐答案

似乎有一些常见的功能和不常见的功能给您的用户类型。如果您的用户类型中存在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.

这篇关于在Django 1.5 / 1.6中设置两种不同类型的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆