Custom User Models का परिचय
In this page:
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 करना कहीं ज़्यादा काम की ज़रूरत करता है।
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.
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 उपयोग करना है।
migrate run करने से पहले set करें — बाद में इसे बदलना एक full database reset की मांग करता है।उदाहरण: Pointing Django at the Custom Model
'accounts.CustomUser' means the CustomUser model lives inside an app named accounts.
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.
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. #}
- Default User पर migrations run करने के बाद एक custom user model में switch करना — Django strongly recommend करता है कि पहली
migrateसे पहलेAUTH_USER_MODELset किया जाए। - Custom model बनाने के बाद
settings.pyमेंAUTH_USER_MODELupdate करना भूल जाना, इसलिए Django default User उपयोग करता रहता है। models.Modelके बजायAbstractUsersubclass न करना, 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 के साथ काम करे।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Introduction to Django's Auth System
- The User Model Overview
- Setting Up a Login View
- Logout Functionality
- User Registration Form
- The login_required Decorator
- The permission_required Decorator
- Django Groups and Permissions
- Password Hashing in Django
- Session Authentication Basics
- Introduction to Custom User Models
- Sending Emails with Django