4

I'm currently designing a Django based site. For simplicity lets assume that it is a simple community site where users can log in and write messages to other users.

My current choice is wether to use the buildin User-Model or to build something my own. I don't need much from the buildin User: there will be no username (you e-mail address is you username), but you an set an internal Name of your choice which can be used by multiple users (like Facebook). Additionally, I don't need the permission system, since access to others will not be based on groups. So I would end up using only the email, firstname, lastname and password fields from the buildin User and everything else would be placed in a UserProfile. On the other hand, the buildin User system will come handy on the backend of the site, since there is the chance I will need a group based permission system there.

All in all, it looks to me, that I rather build my one User Model and use the buildin only for access to the admin backend.

Is there anything wrong with my reflections?

Martin Thurau
  • 7,564
  • 7
  • 43
  • 80

4 Answers4

9

Is there anything wrong with my reflections?

Yes.

My current choice is wether to use the buildin User-Model or to build something my own.

There is a third choice.

http://docs.djangoproject.com/en/1.2/topics/auth/#storing-additional-information-about-users

everything else would be placed in a UserProfile

Correct.

build my one User Model and use the buildin only for access to the admin backend

Don't build your own.

Do this:

If you'd like to store additional information related to your users, Django provides a method to specify a site-specific related model -- termed a "user profile" -- for this purpose.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
2

As the author of django-primate I would like to add some comments. Django-primate which easily lets ju modify the built in User model is meant for just that. You might need just something a little extra, then use django-primate.

But there are problems, although I do not think modifying the django User model per se is a problem at all. One problem is that the "users" are quite different, the admin user and some other user are often not related. This can cause problems when for example an admin is logged in and then wants to login to the site as a "normal user", they do not expect those accounts to be related and do not expect to be logged in automatically as the admin user. This causes headaches for no reason. It also causes a lot of other headaches to implement the recommended related Profile model, you often need to make sure there is a contrib user for every profile and a profile for every contrib user if you for example want to use the authentication decorators. Forms and administration of "users" make this even more cumbersome. In short: usually something will go wrong in this process at some point, it's a curse.

I have mostly abandoned the contrib User model for anything else but for admins. Building another user model is really what you want, but you also want the authenicating part for that user, hence the common use of django contrib User (using it for the wrong reasons). The best solution if you are in a situation like this is to build your own authenication for that custom user model. This is actually quite easy and I cannot recommend this approach enough. I think that the official recommendation is wrong and that there should instead be good tools for authenticating custom user models built into django.

sorl
  • 988
  • 5
  • 6
1

You might want to have a look at the recently created django-primate: https://github.com/aino/django-primate

I once built a custom user model, inheriting from the default one. It works, however, I wouldn't recommend it.

dfoerster
  • 131
  • 6
0

Currently, you have some requirements, but over time they may change. Django's user system is quite straightforward, and using it allows to adapt more easily to some of the most common use cases.
Another aspect to think about, is that there are several applications already available that you can use, and that may require Django's users. Using your own model, may make usage of such modules much more difficult.

On the other hand, hacking the Django's user system in order to comply with your current requirements may be tricky.
Moreover, migrating a 'Custom-User' to a 'Django-User' is always possible, so you are not really closing that door.

Overall, I think it really depends on what you mean with 'user'.
If you mean just a registration, and no real interaction with the core Django features, then I think a separate model is enough, especially because you can migrate at any time, with relatively little effort.
However, if for your application a 'user' maps to something very similar to the what Django is for, then I would use the Django User-Model.

rob
  • 36,896
  • 2
  • 55
  • 65