How do I add custom fields to a User when using django-social-auth
-
28-10-2019 - |
Question
I need to add a BooleanField and a ManyToManyField to my users. I'm using django-social-auth. It seems I could use 'CustomUser'. I guess that's what it's for, but how do I take it into use?
I would need to know:
- where to define these new fields
- How to add them to the new user when the user is created (ie logs in)
- How the query the fields afterwards (ie User.myBooleanField?)
Thanks!
Solution
Create a model called CustomUser or UserProfile, whatever you want, with these fields.
In settings.py add a setting AUTH_PROFILE_MODULE = "account.UserProfile", with what you named your model.
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.
Now anywhere in the site you can call user.get_profile() and you'll have access to these fields.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow