current position:Home>Django model class 2

Django model class 2

2022-01-31 05:37:09 laufing

Model class CRUD

Add data

# Use the object manager of the model class objects.create
User.objects.create(username=xxx,password=xxx,email=xxx,phone=xxx)

# perhaps 
user = User(username=xxx,password=xxx,email=xxx,phone=xxx)
user.save()
 Copy code 

Can be in Django shell Middle attempt , After the code is changed, you need to re-enter

python3 manage.py shell
>>from user.models import User
 Copy code 

Inquire about

User.objects.all() # Query all the data -->QuerySet object , Traverse to get each data object 

# Take values according to fields , Back to QuerySet For the dictionary list 
User.objects.values('col1',"col2",...)
# Back to QuerySet Is a tuple list 
User.objects.values_list("col1","col2")

# Sort 
User.objects.order_by("-age") # Descending , Default ascending order 
User.objects.values("username",'age').order_by("age",'-xx') # according to age Ascending 

QuerySet object .query  # Get sql sentence 
 Copy code 

practice , Configure a route /user/all, Request mode GET, View ,show_all

#views.py
def show_all(request):
	users = User.objects.all()
	return render(request,"index.html",locals())
#locals() Dictionary of function local variables 
 Copy code 

Let the front end display all user data in tabular form

Conditions of the query

User.objects.filter(username="jack",age=12) # Multiple conditions and  Connect , return QuerySet object 

# Exclude queries 
User.objects.exclude(username="jack")# Data excluding this condition , return QuerySet

# Get an object , Returns the object 
User.objects.get(username="jack") 
# Query multiple objects , Report errors 
# If there is no data, an error will be reported , Pay attention to exception capture 

# query predicate 
 Field __ The predicate 
User.objects.filter(id__exact=1) #id by 1
User.objects.filter(id__gt=1) #id>1
User.objects.filter(id__lte=5) #id<=5
User.objects.filter(username__exact="jack") #username  by jack
User.objects.filter(username__contains="jack")#username Contained in the jack
User.objects.filter(username__startswith="j") # With j At the beginning 
User.objects.filter(username__endswith="k")
User.objects.filter(username__in=["jack","tom"])# Is it within the scope of 
User.objects.filter(age__range=(25,35))
 Copy code 

to update

# Update an object 
obj = User.objects.get(username="jack")
obj.age = 30
obj.save()

# Batch update 
queryset = User.objects.filter(age__gt=5)
queryset.update(age=20)
 Copy code 

On the basis of displaying user information in tabular form , Complete the update of user information , Delete the function of two buttons . <a href="book/update/{{ obj.id }}"> to update <a href="book/delete/{{ obj.id }}"> Delete

input Input components : It can't be edited ,disabled="disabled" No submission , Not set up name attribute

Delete

# Delete a single object 
obj = User.objects.get(username="jack")
obj.delete()

# Batch deletion 
queryset = User.objects.filter(age__lt=5)
queryset.delete()

# Pseudo delete , Don't really delete data 
is_delete = models.BooleanField(" Whether or not to delete ",default=False)
# When querying data , Query only is_delete by False The data of 
 Copy code 

Last one :Django Model class 1

copyright notice
author[laufing],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310537073590.html

Random recommended