← Back to Django Course | Chapter 10: Authentication & Authorization | Lesson 11 of 12

Custom User Models का परिचय

एक custom user model अपना खुद का membership card design करने जैसा है बजाय उस generic वाले के जो Django default रूप से देता है।
Syntax
markup
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    extra_field = models.CharField(max_length=100, blank=True)

# settings.py
AUTH_USER_MODEL = 'app_name.CustomUser'

Custom User Model क्यों उपयोग करें

Django recommend करता है कि नए projects को एक custom user model से शुरू किया जाए, एक minimal वाला भी, क्योंकि इसे बाद में swap करना कहीं ज़्यादा काम की ज़रूरत करता है।

Warning: पहले से मौजूद default User के लिए migrations होने के बाद AUTH_USER_MODEL switch करना मुश्किल और risky है।

उदाहरण: Why Use a Custom User Model

AbstractUser already includes username, email, password, and permissions — this only adds one extra field on top.

markup
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    date_of_birth = models.DateField(null=True, blank=True)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Django को Custom Model पर Point करना

AUTH_USER_MODEL Django को बताता है कि authentication के लिए built-in User के बजाय कौन सा model उपयोग करना है।

Note: इसे अपनी पहली migrate run करने से पहले set करें — बाद में इसे बदलना एक full database reset की मांग करता है।

उदाहरण: Pointing Django at the Custom Model

'accounts.CustomUser' means the CustomUser model lives inside an app named accounts.

markup
AUTH_USER_MODEL = 'accounts.CustomUser'
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

User Model को सुरक्षित रूप से Reference करना

get_user_model() हमेशा वही model return करता है जिसकी तरफ AUTH_USER_MODEL point करता है, इसलिए code काम करता रहता है भले ही user model बाद में बदल जाए।

उदाहरण: Referencing the User Model Safely

This code works whether AUTH_USER_MODEL points to the default User or to CustomUser, without any changes.

markup
from django.contrib.auth import get_user_model

User = get_user_model()
new_user = User.objects.create_user(username='carol', password='Pass1234')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Default User पर migrations run करने के बाद एक custom user model में switch करना — Django strongly recommend करता है कि पहली migrate से पहले AUTH_USER_MODEL set किया जाए।
  2. Custom model बनाने के बाद settings.py में AUTH_USER_MODEL update करना भूल जाना, इसलिए Django default User उपयोग करता रहता है।
  3. models.Model के बजाय AbstractUser subclass न करना, built-in auth fields और behavior free में खोते हुए।
चैप्टर सारांश
  • AbstractUser Django के default User को extra fields के साथ extend करता है जबकि सभी existing behavior रखता है।
  • settings.py में AUTH_USER_MODEL custom model की तरफ point करना चाहिए, ideally पहली migration से पहले set किया गया।
  • code में User directly import करने के बजाय get_user_model() उपयोग करना चाहिए, ताकि यह किसी भी user model के साथ काम करे।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.