How To Extend Django User Model: Subscribe To Our Mailing List
How To Extend Django User Model: Subscribe To Our Mailing List
I'm a passionate software developer and researcher. I write about Python, Django and
Web Development on a weekly basis. Read more.
TUTORIAL
The Django’s built-in authentication system is great. For the most part we can use it out-of-the-box, saving a lot of
development and testing effort. It fits most of the use cases and is very safe. But sometimes we need to do some
fine adjustment so to fit our Web application.
Commonly we want to store a few more data related to our User. If your Web application have an social appeal,
you might want to store a short bio, the location of the user, and other things like that.
In this tutorial I will present the strategies you can use to simply extend the default Django User Model, so you
don’t need to implement everything from scratch.
class Person(User):
objects = PersonManager()
class Meta:
proxy = True
ordering = ('first_name', )
def do_something(self):
...
In the example above we have defined a Proxy Model named Person . We tell Django this is a Proxy Model by
adding the following property inside the Meta class: proxy = True .
In this case I’ve redefined the default ordering, assigned a custom Manager to the model, and also defined a new
method do_something .
It is worth noting that User.objects.all() and Person.objects.all() will query the same database table. The
only difference is in the behavior we define for the Proxy Model.
Bear in mind that using this strategy results in additional queries or joins to retrieve the related data. Basically all
the time you access an related data, Django will fire an additional query. But this can be avoided for the most
cases. I will get back to that later on.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
Now this is where the magic happens: we will now define signals so our Profile model will be automatically
created/updated when we create/update User instances.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
Basically we are hooking the create_user_profile and save_user_profile methods to the User model,
whenever a save event occurs. This kind of signal is called post_save .
Generally speaking, you will never have to call the Profile’s save method. Everything is done through the User
model.
Did you know that you can process more than one form at once? Check out this snippet:
forms.py
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('url', 'location', 'company')
views.py
@login_required
@transaction.atomic
def update_profile(request):
if request.method == 'POST':
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
messages.success(request, _('Your profile was successfully updated!'))
return redirect('settings:profile')
else:
messages.error(request, _('Please correct the error below.'))
else:
user_form = UserForm(instance=request.user)
profile_form = ProfileForm(instance=request.user.profile)
return render(request, 'profiles/profile.html', {
'user_form': user_form,
'profile_form': profile_form
})
profile.html
<form method="post">
{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<button type="submit">Save changes</button>
</form>
Oh, right. I’ve addressed this issue in another post named “Optimize Database Queries”. You can read it clicking
here.
But, long story short: Django relationships are lazy. Meaning Django will only query the database if you access one
of the related properties. Sometimes it causes some undesired effects, like firing hundreds or thousands of
queries. This problem can be mitigated using the select_related method.
Knowing beforehand you will need to access a related data, you can prefetch it in a single database query:
users = User.objects.all().select_related('profile')
I had to do it once. Honestly I don’t know if this is the cleaner way to do it, but, here goes nothing:
I needed to use email address as auth token and in the scenario the username was completly useless for me.
Also there was no need for the is_staff flag, as I wasn’t using the Django Admin.
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
def get_full_name(self):
'''
Returns the first_name plus the last_name, with a space in between.
'''
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
'''
Returns the short name for the user.
'''
return self.first_name
I wanted to keep it as close as possible to the existing User model. Since we are inheriting from the
AbstractBaseUser we have to follow some rules:
USERNAME_FIELD: A string describing the name of the field on the User model that is used as the unique
identifier. The field must be unique (i.e., have unique=True set in its definition);
REQUIRED_FIELDS: A list of the field names that will be prompted for when creating a user via the
createsuperuser management command;
is_active: A boolean attribute that indicates whether the user is considered “active”;
get_full_name(): A longer formal identifier for the user. A common interpretation would be the full name of the
user, but it can be any string that identifies the user.
get_short_name(): A short, informal identifier for the user. A common interpretation would be the first name of
the user.
Okay, let’s move forward. I had also to define my own UserManager . That’s because the existing manager define
the create_user and create_superuser methods.
class UserManager(BaseUserManager):
use_in_migrations = True
Basically I’ve done a clean up of the existing UserManager , removing the username and the is_staff property.
Now the final move. We have to update our settings.py. More specifically the AUTH_USER_MODEL property.
AUTH_USER_MODEL = 'core.User'
This way we are telling Django to use our custom model instead the default one. In the example above, I’ve
created the custom model inside an app named core .
class Course(models.Model):
slug = models.SlugField(max_length=100)
name = models.CharField(max_length=100)
tutor = models.ForeignKey(User, on_delete=models.CASCADE)
This is perfectly okay. But if you are creating a reusable app, that you want to make available for the public, it is
strongly advised that you use the following strategy:
class Course(models.Model):
slug = models.SlugField(max_length=100)
name = models.CharField(max_length=100)
tutor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
class User(AbstractUser):
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
AUTH_USER_MODEL = 'core.User'
In a similar way as the previous method, this should be done ideally in the begining of a project and with an extra
care. It will change the whole database schema. Also, prefer to create foreign keys to the User model importing the
settings from django.conf import settings and referring to the settings.AUTH_USER_MODEL instead of referring
directly to the custom User model.
Conclusions
Alright! We’ve gone through four different ways to extend the existing User Model. I tried to give you as much
details as possible. As I said before, there is no best solution. It will really depend on what you need to achieve.
Keep it simple and choose wisely.
Proxy Model: You are happy with everything Django User provide and don’t need to store extra information.
User Profile: You are happy with the way Django handles the auth and need to add some non-auth related
attributes to the User.
Custom User Model from AbstractBaseUser: The way Django handles auth doesn’t fit your project.
Custom User Model from AbstractUser: The way Django handles auth is a perfect fit for your project but still
you want to add extra attributes without having to create a separate Model.
Do NOT hesitate to ask me questions or tell what you think about this post!
You can also join my mailing list. I send exclusive tips directly to your email every week! :-)
Related Posts
What You Should Know About The How to Use JWT Authentication with How to Implement Token
Django User Model Django REST Framework Authentication using Django REST
Framework
Popular Posts
© 2015-2021 simple complex cc by-nc-sa 3.0 // about contact faq cookies privacy policy