Extending, inheriting User Model or custom User Model?

60 views
Skip to first unread message

Tobias Dacoir

unread,
Nov 18, 2014, 10:04:19 AM11/18/14
to django...@googlegroups.com
I'm trying to figure out how to create my Custom User Model.

In the official docs I basically found two different strategies: Extending User Model or Custom User Model: https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#extending-the-existing-user-model

Extending means I have a OnetoOne Relationship to the User Table in my own UserProfile Model, whereas the other method inherits from AbstractBaseUser and looks quite complicated with a lot of warnings that I can't not change this easily after I created my database.

I want to design a web app where users can log in and once log in take some kind of surveys. For this I want to make use of Django's Authentication methods, so only registered and logged in Users can take part in the survey. However I want to save additional data for each User in the database, like Age, Country, Score and so on. In the Admin interface it would be great if I could just select a User Entry and see all fields, the default from the User Model plus my added fields.

It might be a good idea to use OAuth later on as well, to be able to have users authenticate through Facebook and so on, however that is not required right now (I did create quick test project for the social auth plugin and it worked). Still I don't really understand the differences between those two methods and google didn't help me because most tutorials show either route but never say why one is better than the other.

So which should I use in my case?

Daniel Roseman

unread,
Nov 18, 2014, 11:10:36 AM11/18/14
to django...@googlegroups.com
In the case you describe, there's not really a lot to choose between the two approaches. Either you extend the user model with a related profile, in which case you configure the admin to show the profile as an inline underneath the main fields, or you substitute your own model (which probably inherits from AbstractUser), in which case the admin shows all the fields in one section. Personally I prefer the tidiness of custom models, especially as you save a db query every time.
--
DR.

Carl Meyer

unread,
Nov 18, 2014, 11:24:22 AM11/18/14
to django...@googlegroups.com
Hi Tobias & Daniel,
I agree with this advice. Especially because you can't (without great
pain) change the value of AUTH_USER_MODEL after you start the project --
but if you have a custom user model, you can easily generate migrations
to change it (add fields, remove fields, alter fields, etc). So if you
start with the built-in User model, you're stuck with it as-is forever
(unless you use ugly monkeypatch hacks), but if you start out with a
custom User model (even if initially it's no different from the default)
you have more flexibility going forward.

Carl

Tobias Dacoir

unread,
Nov 18, 2014, 1:44:45 PM11/18/14
to django...@googlegroups.com
Dear Daniel and Carl,

thanks for your advice. However I'm a bit confused now. So in general you recommend that I follow the guide for substituting my own custom model but at the same time Carl is warning me that overwriting AUTH_USER_MODEL is quite painful for an existing application (right now I haven't really done much except basic Models and some Views and Forms), but as I read the guide that is exactly what I have to do when using my own custom Model.

I will still try to get it to work somehow. Are all the tutorials found on the internet for Django 1.5 still valid with 1.7?


Carl Meyer

unread,
Nov 18, 2014, 11:02:10 PM11/18/14
to django...@googlegroups.com
Hi Tobias,

On 11/18/2014 11:44 AM, Tobias Dacoir wrote:
> Dear Daniel and Carl,
>
> thanks for your advice. However I'm a bit confused now. So in general
> you recommend that I follow the guide for substituting my own custom
> model but at the same time Carl is warning me that overwriting
> AUTH_USER_MODEL is quite painful for an existing application (right now
> I haven't really done much except basic Models and some Views and
> Forms), but as I read the guide that is exactly what I have to do when
> using my own custom Model.

I thought you were at the beginning of a new project.

If you are still in development, and don't have data in your database
that you care about yet, adding AUTH_USER_MODEL is simple: drop your
database, delete all your migrations, re-create all your initial
migrations, and run them on a brand new database.

> I will still try to get it to work somehow. Are all the tutorials found
> on the internet for Django 1.5 still valid with 1.7?

That would be an ambitious claim to make :-) Since migrations are new in
1.7, anything from an earlier version relating to database creation or
modification (syncdb, migrate, etc) is no longer accurate.

Carl

signature.asc
Reply all
Reply to author
Forward
0 new messages