DigitalOcean Referral Badge
Udit Vashisht
Author: Udit Vashisht


How to extend Django User model using a proxy model?

  • 2 minutes read
  • 2081 Views
How to extend Django User model using a proxy model?

    Table of Contents

There is no doubt that the built-in user authentication system of Django is quite good and caters the common use cases. But sometimes, we do not have our needs met with out-of-the-box defaults django user model. In such a scenario, we can extend the Django User Model using various methods. Such extended models are called Django Custom User Models. In this tutorial, we will be learning to extend the Django User Model using a Proxy Model.

How to extend a Django User Model using a Proxy Model?

This method does not add any extra fields to our User model. The Django Proxy user model only changes the behaviour of an already existing user model from which it inherits. This method is useful if you want to add some methods to your existing user model or change the ordering etc. The database schema is not changed in this method.

You will need to add the following code in your models.py


from django.contrib.auth.models import User
from .managers import PersonManager

class Person(User):
    objects = PersonManager()

    class Meta:
        proxy = True
        ordering = ('first_name', )

    def custom_method(self):
        pass

In th above example, we have created a Django Custom User Model - Person, which inherits the Django User Model, by adding the following property in Meta class:

proxy = True

Then we have changed the ordering by first_name. You must note that though User.objects.all() and Person.objects.all() will call the same database but they will differ in behaviour vis-a-vis the later query will sort the result on the basis of first name.

Such kind of ordering comes handy in blog sites, where you need to show the newest blog post at the top. The same can be implemented as under:-

ordering = ("-updated")

Lastly, in custom_method() you can add some methods to your Custom User Model.


Related Posts

How to Extend Django User model using AbstractUser?
By Udit Vashisht

Even if the default Django User Model caters to all of your needs, it is highly recommended to use a Custom User Model. Django’s official documentation recommends so. Now there are two ways to create a Custom User Model:-

  1. By extending AbstractBaseUser
  2. By extending ...

    Read More
Django Database Migrations: A Comprehensive Overview
By Udit Vashisht

Introduction to Django databases

The Django web framework is designed to work with an SQL-based relational database backend, most commonly PostgreSQL or MySQL. If you’ve never worked directly with a relational database before, managing how your data is stored/accessed and keeping it consistent with your application code ...

Read More
Best way to start a Django project (with Github Integration)
By Udit Vashisht

Correct way to create a Django Project

There are dozens of ways, which developers use to start their Django projects. Possibly, none of them would be a wrong way, but in this post we will be telling you a way which is very helpful and suitable not only in development ...

Read More
Search
Tags
tech tutorials automate python beautifulsoup web scrapping webscrapping bs4 Strip Python3 programming Pythonanywhere free Online Hosting hindi til github today i learned Windows Installations Installation Learn Python in Hindi Python Tutorials Beginners macos installation guide linux SaralGyaan Saral Gyaan json in python JSON to CSV Convert json to csv python in hindi convert json csv in python remove background python mini projects background removal remove.bg tweepy Django Django tutorials Django for beginners Django Free tutorials Proxy Models User Models AbstractUser UserModel convert json to csv python json to csv python Variables Python cheats Quick tips == and is f string in python f-strings pep-498 formatting in python python f string smtplib python send email with attachment python send email automated emails python python send email gmail automated email sending passwords secrets environment variables if name == main Matplotlib tutorial Matplotlib lists pandas Scatter Plot Time Series Data Live plots Matplotlib Subplots Matplotlib Candlesticks plots Tutorial Logging unittest testing python test Object Oriented Programming Python OOP Database Database Migration Python 3.8 Walrus Operator Data Analysis Pandas Dataframe Pandas Series Dataframe index pandas index python pandas tutorial python pandas python pandas dataframe python f-strings padding how to flatten a nested json nested json to csv json to csv python pandas Pandas Tutorial insert rows pandas pandas append list line charts line plots in python Django proxy user model django custom user model django user model matplotlib marker size pytplot legends scatter plot python pandas python virtual environment virtualenv venv python python venv virtual environment in python python decorators bioinformatics fastafiles Fasta python list append append raspberry pi editor cron crontab Cowin Cowin api python dictionary Python basics dictionary python list list ios development listview navigationview swiftui ios mvvm swift environmentobject property wrapper @State @Environm popup @State ios15 alert automation instagram instaloader texteditor youtubeshorts textfield multi-line star rating reusable swift selenium selenium driver requests-html youtube youtube shorts python automation python tutorial algo trading nifty 50 nifty50 stock list nifty50 telegram telegram bot dictionary in Python how to learn python learn python