Django 1.8+扩展了用户模型 [英] Django 1.8+ extending the User model

查看:151
本文介绍了Django 1.8+扩展了用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问到了几百次,但是大部分都是含有接受的答案,这些答案是无效的。其中一些用于Django 1.5,其中一些甚至更老。



所以我正在寻找一个最新的答案。最类似于我的问题的问题是这一个



我正在使用 django-registration 模块,我希望用户有其他字段(如整数 user.points )。我可以实现我自己的 MyUser 模型,并相应地改变我的程序的其余部分。不过,我怀疑这是否符合注册模块。所以我想,理想的方式是继续使用 User 模型,并以某种方式绑定它们,以便每当 User object被创建,相应的 MyUser 对象被创建为附加字段的默认值。



- 我可以感谢任何帮助,

解决方案

p>关于主题的文章:如何扩展Django用户模型



我使用的是一对一方法: / p>

  class UserProfile(models.Model):
user = models.OneToOneField(User,related_name =profile)
#more fields

@staticmethod
@receiver(post_save,sender = User,dispatch_uid =at_user_post_save)
def user_post_save(sender,instance,** kwargs) :
#create new profile on user.save()(如有必要)
profile,new = UserProfile.objects.get_or_create(user = instance)


#in view
profile = request。 user.profile

更新:


是否有其他注意事项?像我删除
UserProfile时会发生什么?


UserProfile 是持有关系的人,所以删除不应该删除用户。当用户通过 on_delete kwarg。


他们总是拥有相同的私钥吗?


没有每个类都有自己的PK,只是 UserProfile 将PK保存到其用户



OneToOneField 在概念上是一个 ForeignKey code> unique = True ,最大的区别是关系的反面不会返回一个含有0/1个元素的列表,但元素本身或者提高 如果 null = True



在这种方法中我不喜欢的唯一的事情是,你总是需要再做一个查询来获取 user.profile 。当新的用户时,我仍然找不到一个好的和干净的方法来始终 .select_related('profile') auth 中获取,但这更多是身份验证系统的问题,而不是方法本身。


I know this question has been asked hundreds of times, but most of them contain -accepted- answers that are not valid anymore. Some of them are for Django 1.5, some of them are even older.

So I'm looking for an up-to-date answer. The question that most resembles my problem is this one.

I'm using the django-registration module and I want users to have additional fields (like integer user.points). I'm OK with implementing my own MyUser model and changing the rest of my program accordingly. However I doubt this will work in compliance with the registration module. So I guess the ideal way would be keep using the User model, and somehow tie them so that whenever a User object is created, a corresponding MyUser object is created with the default values for additional fields.

-How- can I do this with Django 1.8+?

Thanks for any help,

解决方案

An article about the topic: How to Extend Django User Model

What I use is the One-To-One approach:

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name="profile")
    #more fields

    @staticmethod
    @receiver(post_save, sender=User, dispatch_uid="at_user_post_save")
    def user_post_save(sender, instance, **kwargs):
        #create new profile on user.save() (if necessary)
        profile, new = UserProfile.objects.get_or_create(user=instance)


#in view
profile = request.user.profile

UPDATE:

Are there any other caveats? Like what happens when I remove a UserProfile?

UserProfile is the one who holds the relation, so on delete no user should be deleted. You can control what must be the behavior when a user gets deleted via the on_delete kwarg.

Also do they always have the same private keys?

No each class have its own PKs, just the UserProfile holds the PK to its user.

OneToOneField is in conceptually a ForeignKey with an unique=True, the big difference is that the reverse side of the relation do not return a list with 0/1 elements, but the element itself or raise DoesNotExist or None if null=True.

The only thing which I don't like in this approach is that you always have to do 1 more query to get user.profile. I still can't find a good and clean approach to always .select_related('profile') when a new user is fetched from auth, but this is more of a problem of the authentication system rather than the approach itself.

这篇关于Django 1.8+扩展了用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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