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)
, , , , ]>
author_name:小光
***************************filter***************************
]>
author_name:小光
**************************exclude***************************
, , , , ]>
*************************values返回值**************************
orderby按照指定的字段排序
, , , , ]>
************************count统计匹配的数量************************
5
************************first返回第一行数据************************
author_name:小白
************************last返回最后一行数据************************
author_name:小光
******************查询id大于4,小于6的值*******************
]>
****************in查询包括列表中的数或not_in****************
, , ]>
******************contains包含某些值*******************
]>
**********range相当于sql语句中的between and 5<7**********
, , ]>
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)
成都创新互联公司主要从事成都做网站、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务铁西,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575