简体   繁体   English

使用django-social-auth时如何向用户添加自定义字段

[英]How do I add custom fields to a User when using django-social-auth

I need to add a BooleanField and a ManyToManyField to my users. 我需要向用户添加一个BooleanField和ManyToManyField。 I'm using django-social-auth. 我正在使用django-social-auth。 It seems I could use 'CustomUser'. 看来我可以使用“ CustomUser”。 I guess that's what it's for, but how do I take it into use? 我想这就是它的用途,但是我该如何使用它呢?

I would need to know: 我需要知道:

  1. where to define these new fields 在哪里定义这些新字段
  2. How to add them to the new user when the user is created (ie logs in) 创建用户(即登录)后如何将其添加到新用户
  3. How the query the fields afterwards (ie User.myBooleanField?) 之后如何查询字段(即User.myBooleanField?)

Thanks! 谢谢!

Create a model called CustomUser or UserProfile, whatever you want, with these fields. 根据需要,使用这些字段创建一个名为CustomUser或UserProfile的模型。

In settings.py add a setting AUTH_PROFILE_MODULE = "account.UserProfile", with what you named your model. 在settings.py中添加一个设置AUTH_PROFILE_MODULE =“ account.UserProfile”,其中包含您为模型命名的名称。

In the signals for social_auth, make sure the user has a profile, and if not create it for them when the user is created. 在用于social_auth的信号中,确保用户具有配置文件,如果没有,则在创建用户时为其创建配置文件。

Now anywhere in the site you can call user.get_profile() and you'll have access to these fields. 现在,您可以在网站上的任何地方调用user.get_profile(),您将可以访问这些字段。

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

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