all()发方法:
如:
>>> from app1.models import Person
>>> persons=Person.objects.all()
>>> for person in persons:
... print("姓名:",p.name,"\n年龄:",p.age,"\n性别:",p.sex)
...
姓名: lisi
年龄: 25
性别: man
姓名: lisi
年龄: 25
性别: man
>>>
>>>> persons
<QuerySet [<Person: Person object (1)>, <Person: Person object (2)>]>
为了更好的直接显示数据,可以在模型类中实现一个__str__的魔法函数,如模型类代码如下,增加一个魔法函数:
from django.db import models
# Create your models here.
class Person(models.Model):
name=models.CharField("姓名",max_length=50,default='')
age=models.IntegerField("年龄")
sex=models.CharField("性别",max_length=20,default='')
def __str__(self):
return f"姓名:{self.name},年龄:{self.age},性别:{self.sex}"
class Meta:
db_table="person"
然后退出django shell,重新进入django shell,如下:此时可以很清晰的看出all()查询到的对象中的数据
>>> exit()
(django-5F60hEIM) (python39) G:\src\django\mysite1>python manage.py shell
Python 3.9.1 (default, Dec 11 2020, 09:29:25) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app1.models import Person
>>> persons=Person.objects.all()
>>> persons
<QuerySet [<Person: 姓名:zhangsan,年龄:30,性别:man>, <Person: 姓名:lisi,年龄:25,性别:man>]>
>>>
如下:
>>> from app1.models import Person
>>> person=Person.objects.values("name","age")
>>> person
<QuerySet [{'name': 'zhangsan', 'age': 30}, {'name': 'lisi', 'age': 25}]>
>>> for p in person:
... print(p["name"],p["age"])
...
zhangsan 30
lisi 25
>>>
>>> from app1.models import Person
>>> persons=Person.objects.values_list("name","age")
>>> persons
<QuerySet [('zhangsan', 30), ('lisi', 25)]>
>>> for person in persons:
... print(person[0],person[1])
...
zhangsan 30
lisi 25
>>>
>>> from app1.models import Person
>>> persons=Person.objects.order_by("name")
>>> persons
<QuerySet [<Person: 姓名:lisi,年龄:25,性别:man>, <Person: 姓名:zhangsan,年龄:30,性别:man>]>
>>> persons=Person.objects.order_by("-name")
>>> persons
<QuerySet [<Person: 姓名:zhangsan,年龄:30,性别:man>, <Person: 姓名:lisi,年龄:25,性别:man>]>
>>>
只要返回结果是QuerySet,其后可继续叠加使用这些函数的,如:
>>> from app1.models import Person
>>> persons=Person.objects.values("name").order_by("age")
>>> persons
<QuerySet [{'name': 'lisi'}, {'name': 'zhangsan'}]>
>>> persons=Person.objects.values("name").order_by("-age")
>>> persons
<QuerySet [{'name': 'zhangsan'}, {'name': 'lisi'}]>
>>>
通过打印对象.query可以查看到后台执行的SQL语句,如:
>>> from app1.models import Person
>>> persons=Person.objects.values("name").order_by("age")
>>> persons
<QuerySet [{'name': 'lisi'}, {'name': 'zhangsan'}]>
>>> persons.query
<django.db.models.sql.query.Query object at 0x000001CB12117C10>
>>> print(persons.query)
SELECT `person`.`name` FROM `person` ORDER BY `person`.`age` ASC
>>> persons=Person.objects.values("name").order_by("-age")
>>> persons
<QuerySet [{'name': 'zhangsan'}, {'name': 'lisi'}]>
>>> print(persons.query)
SELECT `person`.`name` FROM `person` ORDER BY `person`.`age` DESC
>>>
id | 姓名 | 年龄 | 性别 |
---|---|---|---|
1 | zhangsan | 20 | man |
步骤:
1、在app1应用中编写视图函数如下:
from django.http import HttpResponse
from django.shortcuts import render
from .models import Person
# Create your views here.
def all_person(request):
all_person=Person.objects.all()
return render(request,"app1/all_person.html",locals())
2、在app1/templates/app1/ 目录下创建all_person.html文件,内容如下:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>所有人员信息title>
head>
<body>
<table border="1">
<tr>
<th>idth>
<th>姓名th>
<th>年龄th>
<th>性别th>
tr>
{% for person in all_person %}
<tr>
<th>{{person.id}}th>
<th>{{person.name}}th>
<th>{{person.age}}th>
<th>{{person.sex}}th>
tr>
{% endfor %}
table>
body>
html>
3、在app1应用下的urls.py路由文件中设置如下路由:
4、在主路由文件中注册app1的路由,如下:
5、在浏览器中打开http://127.0.0.1:8080/app1/all_person/ ,结果如下: