简体   繁体   English

Django 1.5多用户身份验证或

[英]Django 1.5 multiple user authentication or

I created a Django 1.5 WebApp with custom User model. 我使用自定义User模型创建了Django 1.5 WebApp。 And it works. 而且有效。 :) :)

I already had a second User2 model that another App was using. 我已经有另一个App正在使用的第二个User2模型。 This second User2 model isn't a Django app so it was built with different criteria. 第二个User2模型不是Django应用,因此它是根据不同的条件构建的。 And it is already in production with many users. 并且已经有许多用户投入生产。

Now I have the need to give both User and User2 the possibility to log-in and do completely different things. 现在,我需要让UserUser2都可以登录并执行完全不同的操作。

I'm thinking about one single Django App to manage everything because in both cases I need to use both models: when User is logged-in I need also some information about User2 and viceversa. 我正在考虑一个单独的Django应用程序来管理所有内容,因为在两种情况下,我都需要使用两种模型:登录User时,我还需要有关User2信息,反之亦然。

I don't see a way to merge User , User2 because they have completely different fields. 我看不到合并UserUser2的方法,因为它们具有完全不同的字段。 Plus User2 has a ForeignKey that points to User . 另外, User2具有指向UserForeignKey

After several trials I discovered that django supports only one AUTH_USER_MODEL . 经过几次试验,我发现django仅支持一个AUTH_USER_MODEL So now I don't know how to proceed. 所以现在我不知道如何进行。

I see the following alternatives: 我看到以下选择:

  • create an abstract User Model ( AbstractUser ) and then subclass User and User2 but I do not know how and if this will work 创建一个抽象的用户模型( AbstractUser ),然后子类化UserUser2但是我不知道它如何以及是否可以工作

  • create a completely new Django WebApp but this require a bigger (and maybe unnecessary) effort 创建一个全新的Django WebApp,但这需要付出更大的努力(也许是不必要的)

  • find another way to authenticate User2 , maybe writing my custom auth code 找到另一种验证User2 ,也许写我的自定义auth代码

Do you see any other option? 您还有其他选择吗? Which is the best? 哪个最好?

Any help would be appreciated! 任何帮助,将不胜感激!

I'm not sure if I understand exactly your problem but if I did I think what you're looking for is a OneToOneField . 我不确定我是否完全了解您的问题,但如果我确实知道您要寻找的是OneToOneField

When I'm working on projects where the clients want customized users, one of the best solution is to create a new model with a OneToOneField pointing to the Django User like a Profile model. 当我在客户需要自定义用户的项目上工作时,最好的解决方案之一是创建一个新模型,其中的OneToOneFieldProfile模型一样指向Django用户。 When you have an User object, you can do user.profile and you get the profile related with user (so you can do user.profile.any_profile_field ). 当您拥有一个User对象时,您可以执行user.profile并获得与该用户相关的配置文件(因此您可以执行user.profile.any_profile_field )。

It goes also in the other way, if you have the object Profile you can access to the User object like profile.user and do also profile.user.first_name 它也以另一种方式运行,如果您有对象Profile ,则可以访问User对象(例如profile.user ,也可以访问profile.user.first_name

So if it fits well with your issue as I think, you should change your model to be a OneToOneField to User like this: 因此,如果按照我的想法,它很适合您的问题,则应将模型更改为对UserOneToOneField ,如下所示:

class Profile(models.Model):
    user = models.OneToOneField(User)
    field1 = models.CharField....
    #     ...    ...     ...
    #      Your fields here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM