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

User Model का Overview

User model Django का ready-made filing card है हर उस person के लिए जो आपकी site में log in कर सकता है।
Syntax
markup
from django.contrib.auth.models import User

user = User.objects.get(username='name')
user.username
user.email
User.objects.filter(is_staff=True)

Default User Fields

Built-in User model में username, email, password, first_name, last_name, is_active, is_staff, is_superuser, और date_joined होते हैं।

उदाहरण: Default User Fields

create_user() is the safe way to make a user — it hashes the password for you instead of storing it raw.

bash
from django.contrib.auth.models import User

user = User.objects.create_user(username='alice', email='[email protected]', password='StrongPass123')
print(user.username)
print(user.email)
print(user.is_active)

⚠️ Run this in your own terminal or Node.js environment.

Users Query करना

चूंकि User एक normal Django model है, आप इसे उन्हीं ORM methods से query करते हैं जो किसी भी और model के लिए उपयोग किए जाते हैं।

उदाहरण: Querying Users

filter() and get() work on User exactly like on any custom model, since it is just a model.

bash
python manage.py shell

>>> from django.contrib.auth.models import User
>>> User.objects.filter(is_active=True).count()
>>> User.objects.get(username='alice')

⚠️ Run this in your own terminal or Node.js environment.

Extra Profile Data Attach करना

चूंकि आपको Django के built-in User model edit नहीं करना चाहिए, extra fields एक separate model से जोड़ें जो एक OneToOneField से linked हो।

Note: यह Django के internal User model को untouched और upgrade-safe रखता है।

उदाहरण: Attaching Extra Profile Data

OneToOneField links exactly one Profile to exactly one User, so user.profile.bio becomes available.

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

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(blank=True)

    def __str__(self):
        return self.user.username
{# 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. Extra fields (जैसे bio या avatar) सीधे Django के built-in User model पर जोड़ना बजाय एक related profile model या custom user model उपयोग करने के।
  2. username को email से confuse करना — default रूप से Django का User log in करने के लिए username चाहता है, email नहीं।
  3. User पर एक non-existent field access करना (जैसे user.name) बजाय असली fields (first_name, last_name) के।
चैप्टर सारांश
  • django.contrib.auth.models.User user accounts के लिए Django का built-in model है।
  • यह username, email, password, first_name, last_name, और is_active जैसे fields के साथ ship होता है।
  • Passwords कभी plain text में store नहीं होते — Django इन्हें अपने आप hash करता है।
  • Extra fields के लिए, User को directly edit करने के बजाय एक separate profile model attach करें।

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.