User Model का Overview
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.
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.
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 हो।
उदाहरण: Attaching Extra Profile Data
OneToOneField links exactly one Profile to exactly one User, so user.profile.bio becomes available.
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. #}
- Extra fields (जैसे bio या avatar) सीधे Django के built-in User model पर जोड़ना बजाय एक related profile model या custom user model उपयोग करने के।
usernameकोemailसे confuse करना — default रूप से Django का User log in करने के लिए username चाहता है, email नहीं।- 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 करें।
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