Default CRUD - ( User Management in Django )
Heading h2
Syntax
Create
from django.contrib.auth.models import User
user = User.objects.create_user(username='myuser', password='mypassword', email='myemail@example.com')
user.first_name = 'John'
user.last_name = 'Doe'
user.save()
Read
from django.contrib.auth.models import User
users = User.objects.all()
for user in users:
print(user.username, user.email)
Update
from django.contrib.auth.models import User
user = User.objects.get(username='myuser')
user.first_name = 'Jane'
user.save()
Delete
from django.contrib.auth.models import User
user = User.objects.get(username='myuser')
user.delete()
Example
Create
from django.contrib.auth.models import User
user = User.objects.create_user(username='johndoe', password='mysecretpassword', email='johndoe@example.com')
user.first_name = 'John'
user.last_name = 'Doe'
user.save()
Read
from django.contrib.auth.models import User
users = User.objects.all()
for user in users:
print(user.username, user.email)
Update
from django.contrib.auth.models import User
user = User.objects.get(username='johndoe')
user.first_name = 'Jane'
user.save()
Delete
from django.contrib.auth.models import User
user = User.objects.get(username='johndoe')
user.delete()
Output
Create
>>> user = User.objects.create_user(username='johndoe', password='mysecretpassword', email='johndoe@example.com')
>>> user.first_name = 'John'
>>> user.last_name = 'Doe'
>>> user.save()
>>> user
<User: johndoe>
Read
johndoe johndoe@example.com
janedoe janedoe@example.com
Update
>>> user = User.objects.get(username='johndoe')
>>> user.first_name = 'Jane'
>>> user.save()
>>> user.first_name
'Jane'
Delete
>>> user = User.objects.get(username='johndoe')
>>> user.delete()
>>> User.objects.all()
<QuerySet [<User: janedoe>]>
Explanation
Django provides default views and templates for CRUD (Create, Read, Update, Delete) operations on user accounts.
The create_user
function can be used to create a new user, along with specifying a username, password, and email. Additional properties like first name and last name can be set using the save
method.
The all
method is used to get all users from the database, which can then be iterated over.
To update a user's properties, the get
method is used to retrieve the user, and then the desired properties are updated and saved using the save
method.
To delete a user, the get
method is used to retrieve the user and then the delete
method is used to remove the user from the database.
Use
The default user management in Django can be used as a starting point for user authentication and authorization in web applications. Developers can customize the built-in user model or create their own models to handle additional user properties and relationships.
Important Points
- Django provides default views and templates for CRUD operations on user accounts
create_user
function is used to create a new user with a username, password, and emailall
method is used to get all users from the databaseget
method is used to retrieve a specific user from the database- Developers can customize the built-in user model or create their own models to handle additional user properties and relationships
Summary
Django's default user management provides a simple and convenient way to handle user authentication and authorization. Developers can use the built-in views and templates for CRUD operations on user accounts or customize them to suit their needs. The default models and views can be extended or replaced with custom models and views as per the requirements of the application.