Django:为custom.user使用自定义类? [英] Django: use custom class for request.user?

查看:86
本文介绍了Django:为custom.user使用自定义类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扩展了Django的默认用户类:

  class CustomUser(User):
friends = model.ManyToManyField('self',symmetric = False)

但是现在我想使用那个,的默认用户类。在我的视图方法中,我可以访问 request.user ,但这是 User 类的一个实例。

解决方案

最简单的方法是使其返回 CustomUser 你也可以在你的models.py中的某个地方的猴子补丁现有的用户模型,它应该只是工作。在你的代码中,只是继续使用原来的类。

  User.add_to_class('friends',models.ManyToManyField('self'对称= False))


I've extended Django's default user class like this:

class CustomUser(User):
    friends = models.ManyToManyField('self', symmetrical=False)

But now I want to use that everywhere instead of the default User class. In my view methods, I have access to request.user but that's an instance of the User class. What's the easiest way to make it return a CustomUser instead?

解决方案

you can also "monkey patch" the existing User model somewhere in your models.py and it should just work. In your code just keep using the original class.

User.add_to_class('friends',models.ManyToManyField('self', symmetrical=False))

这篇关于Django:为custom.user使用自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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