[Django 2021 latest edition tutorial 9] database query operationIn "[Django 2021 Latest Edition Tutorial 8] Operating Mysql Database mysqlclient Installation and Use" introduced the method of inserting new data into the database

1. Add multiple test data

In "[Django 2021 Latest Edition Tutorial 8] Operating Mysql Database mysqlclient Installation and Use" introduced the method of inserting new data into the database

Two, query type

1. Query the total amount of all data

total = User.objects.count()

2. Query the total amount of eligible data

total_condition = User.objects.filter(level=2).count()

3. Query to get all the data

To query all users, use the all() method to query all content. The index subscript can be used to retrieve the object of the model class.

all_user = User.objects.all()

4. Query to get all the data of the specified conditions

all_user_condition = User.objects.filter(level=2)

5. Sorting

Sort by creation time from smallest to largest, -createTime means from largest to smallest

all_user_by_order = User.objects.filter(level=2).order_by('createTime')

6. Query specified data based on id

pk=3 means primary key=3, which is equivalent to id=3. Because id has a special meaning in pycharm, it is the built-in function id() that looks at the memory address, so pk is used.
 a_user_by_id = User.objects.filter(pk=3)

7. Only display specified fields

Only show the two fields of id and name
just_show_some_fields = User.objects.filter(pk=3).values("pk", "name")

8. For more inquiries, please refer to

https://www.runoob.com/django/django-orm-1.html

Three, the effect

1. Add testQueryUser function

import datetimeimport json from django.core import serializersfrom django.http import HttpResponse, JsonResponsefrom django.shortcuts import render # Create your views here.from userWeb.models import User  def index(request):    return render(request, './userWeb/index.html') # 加法页面def add(request):    return render(request, './userWeb/add.html') # 执行加法def doadd(request):    a = request.POST['a']    b = request.POST['b']    a = int(a)    b = int(b)    result = a + b    # return HttpResponse(str(result))    context = {}    context['a'] = a    context['b'] = b    context['result'] = result    return render(request, './userWeb/add_result.html', context) # 增加用户页面def testUser(request):    return render(request, './userWeb/testUser.html') # 执行增加用户def testAddUser(request):     name = request.POST['name']    level = request.POST['level']    createTime = datetime.datetime.now()     user = User.objects.create(name=name, level=level, createTime=createTime)     context = {}    context['msg'] = '用户新增成功'    context['数据库中的id'] = user.id    return HttpResponse(str(context)) # 数据库查询def testQueryUser(request):     total = User.objects.count() # 查询所有数据的总量    total_condition = User.objects.filter(level=2).count() # 查询符合条件的数据总量    all_user = User.objects.all() # 查询得到所有用户,使用 all() 方法来查询所有内容。可用索引下标取出模型类的对象。    all_user_condition = User.objects.filter(level=2) # 查询得到指定条件的所有用户    all_user_by_order = User.objects.filter(level=2).order_by('createTime') # 按创建时间从小到大排序,-createTime表示从大到小    a_user_by_id = User.objects.filter(pk=3)  #pk=3 的意思是主键 primary key=3,相当于 id=3。因为 id 在 pycharm 里有特殊含义,是看内存地址的内置函数 id(),因此用 pk。    just_show_some_fields = User.objects.filter(pk=3).values("pk", "name")  # 只显示id和name者两个字段    print(just_show_some_fields)     context = {}    context['total'] = total # 查询所有数据的总量    context['total_condition'] = total_condition # 查询符合条件的数据总量    context['all_user'] = serializers.serialize("json", all_user) # 查询得到所有用户,使用 all() 方法来查询所有内容。可用索引下标取出模型类的对象。    context['all_user_condition'] = serializers.serialize("json", all_user_condition) # 查询得到指定条件的所有用户    context['all_user_by_order'] = serializers.serialize("json", all_user_by_order) # 按创建时间从小到大排序,-createTime表示从大到小    context['a_user_by_id'] = serializers.serialize("json", a_user_by_id)  #pk=3 的意思是主键 primary key=3,相当于 id=3。因为 id 在 pycharm 里有特殊含义,是看内存地址的内置函数 id(),因此用 pk。     #objects.values()返回django.db.models.query.QuerySet对象,需要将ValuesQuerySet对象需要先转换成list    context['just_show_some_fields'] = json.dumps(list(just_show_some_fields))  # 只显示id和name者两个字段     return JsonResponse(context)

2. Modify urls.py

3. Operation effect