import os
if __name__ == '__main__':
#加載Django項(xiàng)目的配置信息
os.environ.setdefault("DJANGO_SETTINGS_MODULE","testinclude.settings")
#導(dǎo)入Django,并啟動(dòng)Django項(xiàng)目
import django
django.setup()
from ormtest import models
#查詢(xún)所有作者
all_author = models.Author.objects.all()
print(all_author)
#get查詢(xún)某個(gè)人,當(dāng)值不存在時(shí)會(huì)報(bào)錯(cuò)
person = models.Author.objects.get(id=7)
print(person)
print("filter".center(60,"*"))
#filter返回?cái)?shù)據(jù)為一個(gè)列表,當(dāng)值不存在時(shí),不會(huì)報(bào)錯(cuò),返回一個(gè)空的列表
person = models.Author.objects.filter(id=7)
print(person)
print(person[0])
#exclude 返回id不等于1的所有值,排隊(duì)法
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統(tǒng)計(jì)匹配的數(shù)量".center(60,"*"))
count = models.Author.objects.all().count()
print(count)
print("first返回第一行數(shù)據(jù)".center(60,"*"))
person = models.Author.objects.all().first()
print(person)
print("last返回最后一行數(shù)據(jù)".center(60, "*"))
person = models.Author.objects.all().last()
print(person)
print("查詢(xún)id大于4,小于6的值".center(50,"*"))
person = models.Author.objects.filter(id__gt=5,id__lt=7)
print(person)
print("in查詢(xún)包括列表中的數(shù)或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相當(dāng)于sql語(yǔ)句中的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統(tǒng)計(jì)匹配的數(shù)量************************
5
************************first返回第一行數(shù)據(jù)************************
author_name:小白
************************last返回最后一行數(shù)據(jù)************************
author_name:小光
******************查詢(xún)id大于4,小于6的值*******************
]>
****************in查詢(xún)包括列表中的數(shù)或not_in****************
, , ]>
******************contains包含某些值*******************
]>
**********range相當(dāng)于sql語(yǔ)句中的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)
創(chuàng)新互聯(lián)公司主營(yíng)壽光網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā)公司,壽光h5成都小程序開(kāi)發(fā)搭建,壽光網(wǎng)站營(yíng)銷(xiāo)推廣歡迎壽光等地區(qū)企業(yè)咨詢(xún)另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。