I need to create a custom User model for authentication backend. SSurfing the internet, I noticed that people create two models: UserProfile and User. So, as I understand, UserProfile contains an inheritence of standart User model and some additional fields. (https://stackoverflow.com/a/6092091/4132352) So, there is a question then: what does contain User model then? Or do I need only one model?
3 Answers
No, you've misunderstood a couple of things.
Prior to Django 1.5, there was no way to replace or extend the built-in User model. So if you wanted to add extra fields, the recommended solution was to provide a UserProfile that had a one-to-one relationship with User.
Since Django 1.5, it has been possible to define your own user model and tell Django to use it in place of the built-in one. So now the recommendation is to do this rather than creating a UserProfile. That is what you should do: define your model that inherits from AbstractUser or AbstractBaseUser, and point settings.AUTH_USER_MODEL to that.

- 588,541
- 66
- 880
- 895
-
Thank you. I've seen that custom user inherits from User, not from AbstractUser. class MyUser(User): user = models.OneToOneField(User) – andriy Oct 21 '14 at 21:30
-
1Well, that's just mad, since you now have two relationships from MyUser to User, inheritance and one-to-one, both of which use their own unique foreign key fields. Neither of these is needed: please don't do this. – Daniel Roseman Oct 21 '14 at 21:34
-
Can I ask you one more question, but one by one in chat? – andriy Oct 21 '14 at 21:37
-
Or here. Look: I want to make an authentication backend, because I have another source of usernames and passwords( an existing table in DB). How to implement it in my backend? I've found no information about it here: https://docs.djangoproject.com/en/dev/topics/auth/customizing/ – andriy Oct 21 '14 at 21:43
-
I'm sorry, why is the section on "Other authentication sources" at the top of that page not exactly what you are after? – Daniel Roseman Oct 21 '14 at 21:52
-
I can see that, but in that article there is no explanaition, how to point to other source? – andriy Oct 21 '14 at 21:59
-
It just tells that we can do it, but no word about how to do it. – andriy Oct 21 '14 at 21:59
-
I can't see what else you could possibly need to know. Define the backend with an authenticate method that checks the username and password in your external database, and return a User object. That's literally everything you need to do. – Daniel Roseman Oct 21 '14 at 22:18
-
I just can do it performing SELECT query with cursor, so that to check if such username with such password exists. But why do I need extended model then? – andriy Oct 21 '14 at 22:28
-
How to connect my table in DB with my extended user model? – andriy Oct 21 '14 at 22:32
-
" How to connect my table in DB with my extended user model? " When you will create new model like try to point yours db_table in class Meta of UserModel, like this: `class CustomUser(AbstactBaseUser): pass class Meta(AbstractBaseUser.Meta): db_table = 'custom_user_table_name'` – Kyrylo Perevozchikov Oct 21 '14 at 23:28
You only need one User model it's built into all of the authentication logic built into Django. UserProfile should be a customized table which has a foreign key relationship to the User. Furthermore you should make sure anytime a new user object is created a userprofile is created as well.
This video does a good job explaining how to go about doing that.

- 11,923
- 6
- 58
- 68
Basically you subclass the user model now and let your settings know you are going to use a custom model. You can read in detail about it in documentation
the big picture is:
Create the model that extends from user:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
id_number = models.CharField(max_length=255, blank=True)
Let your settings module know you are going to use this model as your user model (settings.py):
AUTH_USER_MODEL = "users.User"
When you are going to reference it in another model make sure you are pointing to this class:
class OtherModel(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
As an alternative, create an app for users and import it like you would with any other model:
from users.models import User
class OtherModel(models.Model):
user = models.ForeignKey(User)

- 2,021
- 1
- 18
- 26