Extending the user model

When you have to deal with user accounts, you will find that the user model of the Django authentication framework is suitable for common cases. However, the user model comes with very basic fields. You may wish to extend the user model to include additional data. The best way to do this is by creating a profile model that contains all additional fields and a one-to-one relationship with the Django user model.

Edit the models.py file of your account application and add the following code to it:

from django.db import modelsfrom django.conf import settingsclass Profile(models.Model):    user = models.OneToOneField(settings.AUTH_USER_MODEL,                                on_delete=models.CASCADE) date_of_birth = models.DateField(blank=True, null=True) ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.