Django 1.5多用户身份验证或
I created a Django 1.5 WebApp with custom User
model. And it works. :)
我用自定义用户模型创建了Django 1.5 WebApp。和它的工作原理。:)
I already had a second User2
model that another App was using. This second User2
model isn't a Django app so it was built with different criteria. And it is already in production with many users.
我已经有了第二个User2模型,另一个应用正在使用。第二个User2模型不是Django应用程序,所以它是用不同的标准构建的。它已经在生产中,有很多用户。
Now I have the need to give both User
and User2
the possibility to log-in and do completely different things.
现在我有必要让用户和用户都可以登录,做完全不同的事情。
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应用程序来管理一切,因为在这两种情况下我都需要使用这两个模型:当用户登录时,我还需要一些关于User2和viceversa的信息。
I don't see a way to merge User
, User2
because they have completely different fields. Plus User2
has a ForeignKey
that points to User
.
我看不到合并User的方法,User2因为它们有完全不同的字段。加上User2有一个指向User的ForeignKey。
After several trials I discovered that django supports only one AUTH_USER_MODEL
. So now I don't know how to proceed.
经过几次测试,我发现django只支持一个AUTH_USER_MODEL。所以现在我不知道该怎么继续。
I see the following alternatives:
我看到了以下选择:
create an abstract User Model (
AbstractUser
) and then subclassUser
andUser2
but I do not know how and if this will work创建一个抽象的用户模型(AbstractUser),然后子类化User和User2,但是我不知道它是如何工作的,也不知道它是否能够工作
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的方法,可能是编写自定义身份验证代码
Do you see any other option? Which is the best?
你还有别的选择吗?哪个是最好的?
Any help would be appreciated!
如有任何帮助,我们将不胜感激!
1 个解决方案
#1
1
I'm not sure if I understand exactly your problem but if I did I think what you're looking for is a 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. 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
).
当我处理客户需要定制用户的项目时,最好的解决方案之一是创建一个新模型,其中一个toonefield指向Django用户,就像一个概要模型一样。当您有一个User对象时,您可以执行User。配置文件和获取与用户相关的配置文件(因此可以使用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
另一种方式是,如果你有对象配置文件,你可以访问用户对象,比如概要文件。user和do也包含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:
因此,如果它符合你的问题,如我所认为的,你应该改变你的模型,成为一个单一领域的用户,像这样:
class Profile(models.Model):
user = models.OneToOneField(User)
field1 = models.CharField....
# ... ... ...
# Your fields here
更多相关文章
- Python脚本如何获取当前环节和用户等信息
- Django代理模型返回父模型
- Tensorflow:恢复图形和模型,然后在单个图像上运行评估
- python接入微博第三方API之2接入用户登录和微博发布
- linux修改文件所属用户和组
- [caffe(二)]Python加载训练caffe模型并进行测试2
- shell用户配置详解及bash命令参数
- linux用户管理及用户权力下放
- Linux学习笔记(九)--RedHat 7.0之用户身份与文件权限、存储结构与