Django 之 ORM 查询使有

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 之 ORM 查询使有相关的知识,希望对你有一定的参考价值。

一、在Django项目上运行python文件查询数据库
import os

if __name__ == ‘__main__‘:
    #加载Django项目的配置信息
    os.environ.setdefault("DJANGO_SETTINGS_MODULE","testinclude.settings")
    #导入Django,并启动Django项目
    import django
    django.setup()

    from ormtest import models

    #查询所有作者
    all_author = models.Author.objects.all()
    print(all_author)

    #get查询某个人,当值不存在时会报错
    person = models.Author.objects.get(id=7)
    print(person)

    print("filter".center(60,"*"))
    #filter返回数据为一个列表,当值不存在时,不会报错,返回一个空的列表
    person = models.Author.objects.filter(id=7)
    print(person)
    print(person[0])

    #exclude 返回id不等于1的所有值,排队法
    print("exclude".center(60,"*"))
    person = models.Author.objects.exclude(id=1)
    print(person)

    print("values返回值".center(60,"*"))
    person = models.Author.objects.values()
    print(person)

    print("orderby按照指定的字段排序")
    person = models.Author.objects.all().order_by("id")
    print(person)

    print("count统计匹配的数量".center(60,"*"))
    count = models.Author.objects.all().count()
    print(count)

    print("first返回第一行数据".center(60,"*"))
    person = models.Author.objects.all().first()
    print(person)

    print("last返回最后一行数据".center(60, "*"))
    person = models.Author.objects.all().last()
    print(person)

    print("查询id大于4,小于6的值".center(50,"*"))
    person = models.Author.objects.filter(id__gt=5,id__lt=7)
    print(person)

    print("in查询包括列表中的数或not_in".center(50,"*"))
    person = models.Author.objects.filter(id__in=[3,5,7])
    print(person)

    print("contains包含某些值".center(50,"*"))
    person = models.Author.objects.filter(name__contains="周")
    print(person)

    print("range相当于sql语句中的between and 5<7".center(50,"*"))
    person = models.Author.objects.filter(id__range=[5,7])
    print(person)

程序执行结果:

<QuerySet [<Author: author_name:小白>, <Author: author_name:小周>, <Author: author_name:小红>, <Author: author_name:小绿>, <Author: author_name:小光>]>
author_name:小光
***************************filter***************************
<QuerySet [<Author: author_name:小光>]>
author_name:小光
**************************exclude***************************
<QuerySet [<Author: author_name:小白>, <Author: author_name:小周>, <Author: author_name:小红>, <Author: author_name:小绿>, <Author: author_name:小光>]>
*************************values返回值**************************
<QuerySet [{‘id‘: 3, ‘name‘: ‘小白‘}, {‘id‘: 4, ‘name‘: ‘小周‘}, {‘id‘: 5, ‘name‘: ‘小红‘}, {‘id‘: 6, ‘name‘: ‘小绿‘}, {‘id‘: 7, ‘name‘: ‘小光‘}]>
orderby按照指定的字段排序
<QuerySet [<Author: author_name:小白>, <Author: author_name:小周>, <Author: author_name:小红>, <Author: author_name:小绿>, <Author: author_name:小光>]>
************************count统计匹配的数量************************
5
************************first返回第一行数据************************
author_name:小白
************************last返回最后一行数据************************
author_name:小光
******************查询id大于4,小于6的值*******************
<QuerySet [<Author: author_name:小绿>]>
****************in查询包括列表中的数或not_in****************
<QuerySet [<Author: author_name:小白>, <Author: author_name:小红>, <Author: author_name:小光>]>
******************contains包含某些值*******************
<QuerySet [<Author: author_name:小周>]>
**********range相当于sql语句中的between and 5<7**********
<QuerySet [<Author: author_name:小红>, <Author: author_name:小绿>, <Author: author_name:小光>]>

models.py文件代码

from django.db import models

# Create your models here.

class Publisher(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64,null=False,unique=True)

    def __str__(self):
        return "publisher_name:{}".format(self.name)

class Book(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=128,null=False)
    publisher = models.ForeignKey(to=Publisher)

    def __str__(self):
        return "book_title:{}".format(self.title)

class Author(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=16,null=False)
    book = models.ManyToManyField(to="Book")

    def __str__(self):
        return "author_name:{}".format(self.name)

技术图片

技术图片

以上是关于Django 之 ORM 查询使有的主要内容,如果未能解决你的问题,请参考以下文章

(Django)气流中的 ORM - 有可能吗?

Django之ORM查询进阶

Django之orm查询

django知识之ORM查询

Django之ORM查询复习与cookie

python测试开发django-76.ORM查询之Q查询