Table of Contents
1. Add, delete, modify and check data
1. Display of user list
2. Modify the logical analysis of data
3. Analysis of deletion function
2. How to create table relationships
3. Django’s request life cycle flow chart
1. Add, delete, modify and check data
1. Display of user list
Query all user data in the data table and display it on the page
Query data
def userlist(request): """Display user data""" # 1. Query the data table first """There is no place for negative slices here""" # user_list=models.UserInfo.objects.all()[0:2] # Query all data # user_list=models.UserInfo.objects.first() # Query all data # select *from userinfo where username ='kevin'; # filter: Use of analogy where # As long as the returned result is a queryset object, you can keep clicking on the method. # user_list=models.UserInfo.objects.filter(username='kevin').first() # Query all data user_list=models.UserInfo.objects.all() # Query all data # The queryset object is in the form of a list of objects # <QuerySet [<UserInfo: UserInfo object>, <UserInfo: UserInfo object>, <UserInfo: UserInfo object>]> # print(user_list[0]) # print(user_list[0].username) # print(user_list[0].password) # print(user_list[0].gender) # print(user_list[1].gender) '''Support for loop''' # for i in user_list: # print(i.gender) return render(request, 'userlist.html', locals()) def edit(request): """Receive the id value first""" edit_id = request.GET.get('id') """Based on this id value, query the current data in the table again""" # <QuerySet [<UserInfo: kevin>]> edit_obj=models.UserInfo.objects.filter(id=edit_id).first() if request.method == 'POST': hidden_id = request.POST.get('hidden_id') username = request.POST.get('username') password = request.POST.get('password') gender = request.POST.get('gender')
2. Modify the logical analysis of the data
- First determine which record to modify ——> How to determine which record to modify?
- What field can be used to determine the unique record? The primary key id must be carried to the backend.
- Receive the primary key id value at the back end, and then query the data in the table based on this id value to query a
- Then render the queried data into the page, and then modify it
- Submit the modified form to the backend and make modifications
Data modification
# Modification of the first method affect_rows=models.UserInfo.objects.filter(pk=hidden_id).update(username=username, password=password, gender=gender) # print(affect_rows) # Modification of the second method edit_obj.username=username edit_obj.password=password edit_obj.gender=gender edit_obj.save() # Save data # Jump address to list display return redirect('/userlist/') return render(request, 'edit.html', locals())
Adding data
def delete(request): """1. You still need to receive the primary key id value passed from the front end""" delete_id = request.GET.get('id') # delete from userinfo where id = 1; # models.UserInfo.objects.filter(pk=delete_id).delete() # user_obj = models.UserInfo.objects.filter(pk=delete_id).first() # user_obj.delete() return redirect('/userlist/') def add(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') gender = request.POST.get('gender') """Operation data volume add data""" # Returns the object of the current record # user_obj=models.UserInfo.objects.create(username=username, password=password, gender=gender) # print(user_obj) # pyy # print(user_obj.gender) # print(user_obj.pk) """Second way to add""" user_obj = models.UserInfo(username=username,password=password,gender=gender) user_obj.save() return redirect('/userlist/') return render(request, 'add.html')
3. Analysis of deletion function
- Add a link to the delete button, carrying the id value of the current record
- The backend needs to receive this primary key id value
- The backend directly performs the delete operation
Deletion of data
2. How to create a table relationship
- One to one
- one to many
- many to many
- It doesn’t matter
Take the library management system as an example
- book table
- Publisher list
- Author list
- Author details table
Put yourself in someone else’s shoes and judge the relationship
- Book table and publishing house table >>> One to many >>> Book table is many, publishing house is one >>> Built on the side of many
- Books table and author table >>> Many-to-many >>> A third table is needed
- Author table and author details table >>> One-to-one >>> Foreign key fields are generally built in areas with higher query frequency.
How to create table relationships in Django
class Book(models.Model): title = models.CharField(max_length=64) """ max_digits=None,:Total number of digits decimal_places=None: number of decimal places """ # price decimal(8,2) price = models.DecimalField(max_digits=8, decimal_places=2) # publish_id = models.ForeignKey(to='Publish', to_field='id') """For foreign key field relationships, _id will be automatically spliced for us""" publish = models.ForeignKey(to='Publish') """authors is a virtual field, it will not actually create this field in the table. This sentence can automatically help us create the third table """ authors = models.ManyToManyField(to='Author')
"""Publishing List""" class Publish(models.Model): name = models.CharField(max_length=64) addr = models.CharField(max_length=64)
"""Author list""" class Author(models.Model): name = models.CharField(max_length=64) author_detail = models.OneToOneField(to='AuthorDetail')
"""Author details table""" class AuthorDetail(models.Model): phone = models.CharField(max_length=64) email = models.CharField(max_length=64)