简体   繁体   English

使用Django User-Model还是创建自己的Model?

[英]Use Django User-Model or create a own Model?

I'm currently designing a Django based site. 我目前正在设计一个基于Django的网站。 For simplicity lets assume that it is a simple community site where users can log in and write messages to other users. 为简单起见,我们假设它是一个简单的社区站点,用户可以登录并向其他用户写入消息。

My current choice is wether to use the buildin User-Model or to build something my own. 我目前的选择是使用buildin用户模型或构建我自己的东西。 I don't need much from the buildin User : there will be no username (you e-mail address is you username), but you an set an internal Name of your choice which can be used by multiple users (like Facebook). 我不需要很多来自buildin User :没有用户名(你的电子邮件地址是你的用户名),但你设置了一个你可以选择的内部名称,可供多个用户(如Facebook)使用。 Additionally, I don't need the permission system, since access to others will not be based on groups. 此外,我不需要权限系统,因为访问其他人不会基于组。 So I would end up using only the email, firstname, lastname and password fields from the buildin User and everything else would be placed in a UserProfile. 因此,我最终只使用buildin User的电子邮件,名字,姓氏和密码字段,其他所有内容都将放在UserProfile中。 On the other hand, the buildin User system will come handy on the backend of the site, since there is the chance I will need a group based permission system there. 另一方面,buildin用户系统将在网站的后端派上用场,因为我有可能需要一个基于组的权限系统。

All in all, it looks to me, that I rather build my one User Model and use the buildin only for access to the admin backend. 总而言之,在我看来,我宁愿构建我的一个用户模型并仅使用buildin来访问管理员后端。

Is there anything wrong with my reflections? 我的思考有什么不对吗?

Is there anything wrong with my reflections? 我的思考有什么不对吗?

Yes. 是。

My current choice is wether to use the buildin User-Model or to build something my own. 我目前的选择是使用buildin用户模型或构建我自己的东西。

There is a third choice. 还有第三种选择。

http://docs.djangoproject.com/en/1.2/topics/auth/#storing-additional-information-about-users http://docs.djangoproject.com/en/1.2/topics/auth/#storing-additional-information-about-users

everything else would be placed in a UserProfile 其他一切都将放在UserProfile中

Correct. 正确。

build my one User Model and use the buildin only for access to the admin backend 构建我的一个用户模型并仅使用buildin来访问管理员后端

Don't build your own. 不要建立自己的。

Do this: 做这个:

If you'd like to store additional information related to your users, Django provides a method to specify a site-specific related model -- termed a "user profile" -- for this purpose. 如果您想存储与您的用户相关的其他信息,Django提供了一种方法来指定特定于站点的相关模型 - 称为“用户配置文件” - 用于此目的。

As the author of django-primate I would like to add some comments. 作为django-primate的作者,我想补充一些评论。 Django-primate which easily lets ju modify the built in User model is meant for just that. Django-primate很容易让ju修改内置的User模型就是为了这个。 You might need just something a little extra, then use django-primate. 您可能只需要一些额外的东西,然后使用django-primate。

But there are problems, although I do not think modifying the django User model per se is a problem at all. 但是有问题,虽然我不认为修改django用户模型本身就是一个问题。 One problem is that the "users" are quite different, the admin user and some other user are often not related. 一个问题是“用户”非常不同,管理员用户和一些其他用户通常不相关。 This can cause problems when for example an admin is logged in and then wants to login to the site as a "normal user", they do not expect those accounts to be related and do not expect to be logged in automatically as the admin user. 例如,当管理员登录然后想要以“普通用户”身份登录网站时,这可能会导致问题,他们不希望这些帐户相关,也不希望以管理员用户身份自动登录。 This causes headaches for no reason. 这无缘无故会导致头痛。 It also causes a lot of other headaches to implement the recommended related Profile model, you often need to make sure there is a contrib user for every profile and a profile for every contrib user if you for example want to use the authentication decorators. 它还会导致许多其他令人头疼的事情来实现推荐的相关Profile模型,如果您想要使用身份验证装饰器,通常需要确保每个配置文件都有一个contrib用户和每个contrib用户的配置文件。 Forms and administration of "users" make this even more cumbersome. “用户”的形式和管理使这更加麻烦。 In short: usually something will go wrong in this process at some point, it's a curse . 简而言之: 通常在这个过程中某些事情会出错,这是一个诅咒

I have mostly abandoned the contrib User model for anything else but for admins. 除了管理员之外,我大多放弃了contrib用户模型。 Building another user model is really what you want, but you also want the authenicating part for that user, hence the common use of django contrib User (using it for the wrong reasons). 构建另一个用户模型实际上是你想要的,但你也想要该用户的身份验证部分,因此django contrib User的常见用法(出于错误的原因使用它)。 The best solution if you are in a situation like this is to build your own authenication for that custom user model. 如果您遇到这种情况,最好的解决方案是为该自定义用户模型构建自己的身份验证。 This is actually quite easy and I cannot recommend this approach enough. 这实际上非常简单,我不能推荐这种方法。 I think that the official recommendation is wrong and that there should instead be good tools for authenticating custom user models built into django. 我认为官方推荐是错误的,而且应该有很好的工具来验证django中内置的自定义用户模型。

You might want to have a look at the recently created django-primate: https://github.com/aino/django-primate 你可能想看看最近创建的django-primate: https//github.com/aino/django-primate

I once built a custom user model, inheriting from the default one. 我曾经构建了一个自定义用户模型,继承自默认模型。 It works, however, I wouldn't recommend it. 但它有效,我不推荐它。

Currently, you have some requirements, but over time they may change. 目前,您有一些要求,但随着时间的推移它们可能会发生变化。 Django's user system is quite straightforward, and using it allows to adapt more easily to some of the most common use cases. Django的用户系统非常简单,使用它可以更容易地适应一些最常见的用例。
Another aspect to think about, is that there are several applications already available that you can use, and that may require Django's users. 要考虑的另一个方面是,您可以使用几种已经可用的应用程序,这可能需要Django的用户。 Using your own model, may make usage of such modules much more difficult. 使用您自己的模型可能会使这些模块的使用变得更加困难。

On the other hand, hacking the Django's user system in order to comply with your current requirements may be tricky. 另一方面,为了符合您当前的要求而攻击Django的用户系统可能会非常棘手。
Moreover, migrating a 'Custom-User' to a 'Django-User' is always possible, so you are not really closing that door. 此外,始终可以将“自定义用户”迁移到“Django用户”,因此您并没有真正关闭那扇门。

Overall, I think it really depends on what you mean with 'user'. 总的来说,我认为这取决于你对'用户'的意思。
If you mean just a registration, and no real interaction with the core Django features, then I think a separate model is enough, especially because you can migrate at any time, with relatively little effort. 如果你只是注册,并没有与核心Django功能的真正交互,那么我认为一个单独的模型就足够了,特别是因为你可以在任何时候以相对较少的努力进行迁移。
However, if for your application a 'user' maps to something very similar to the what Django is for, then I would use the Django User-Model. 但是,如果对于您的应用程序,“用户”映射到与Django非常相似的东西,那么我将使用Django用户模型。

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

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