您好,登錄后才能下訂單哦!
?
目錄
queries:... 1
exclude():... 3
關聯對象查詢:... 3
反向查詢對<field-name>_set改名:... 4
復雜查詢:... 4
保存ForeignKey和ManyToMany字段:... 5
QuerySet鏈式過濾:... 5
lookup,高級條件過濾:... 5
跨關聯關系查詢:... 6
限制返回個數:... 6
查詢對象比較... 7
不常用查詢API:... 7
事務:... 10
自定義管理器:... 11
?
?
?
?
user.groups.set(groups)
user.groups.all()
group.user_set.all()?? #反查會加上_set
?
Author.objects.exclude(name='jowin')
?
Entry.objects.get(id=1).blog?? #一對多,前向查詢,通過屬性訪問關聯的(外部)對象
Blog.objects.get(id=1).entry_set.all()?? #一對多,反向查詢,模型中有fk,該fk所指的模型實例可通過一個管理器返回前一個模型的所有實例,默認這個管理器的名字是<field-name>_set,<field-name>是源模型的小寫名稱,該管理器返回的查詢集可用get、filter、exclude再次操作;如Course和Lesson,Lesson中的course = models.ForeignKey(Course, verbose_name='課程'),在Course中可定義方法def get_lesson(self): return self.lesson_set.all()
?
?
例:
class Group(models.Model):
??? name = models.CharField(max_length=20)
?
??? def __str__(self):
??????? return self.name
?
class User(models.Model):
??? name = models.CharField(max_length=20)
?? ?groups = models.ManyToManyField(Group)?? #ManyToMany會自動生成中間表publish_user_groups
?
??? def __str__(self):
??????? return self.name
?
>>> from publish.models import Group,User
>>> user = User.objects.create(name='jowin')
>>> group1 = Group.objects.create(name='magedu1')
>>> group2 = Group.objects.create(name='magedu2')
>>> user = User.objects.first()
>>> groups = Group.objects.all()
>>> type(groups)
<class 'django.db.models.query.QuerySet'>
>>> user.groups.set(groups)?? #將用戶為'jowin'加入多個組里
>>> user.groups.all()
<QuerySet [<Group: magedu1>, <Group: magedu2>]>
>>> group = Group.objects.get(id=1)
>>> group.user_set.all()?? #反查會加上_set
<QuerySet [<User: jowin>]>
?
?
例,多對多,自己加中間表:
class Group(models.Model):
??? name = models.CharField(max_length=20)
?
??? def __str__(self):
??????? return self.name
?
class User(models.Model):
??? name = models.CharField(max_length=20)
??? # groups = models.ManyToManyField(Group)
?
??? def __str__(self):
??????? return self.name
?
class UserGroupRelation(models.Model):?? #多對多,自己加中間表
??? user = models.ForeignKey(User)
??? group = models.ForeignKey(Group)
?
sqlite> .schema publish_usergrouprelation
CREATE TABLE IF NOT EXISTS "publish_usergrouprelation" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "group_id" integer NOT NULL REFERENCES "publish_group
" ("id"), "user_id" integer NOT NULL REFERENCES "publish_user" ("id"));
CREATE INDEX "publish_usergrouprelation_group_id_e393f98c" ON "publish_usergrouprelation" ("group_id");
CREATE INDEX "publish_usergrouprelation_user_id_0e041f81" ON "publish_usergrouprelation" ("user_id");
sqlite> .schema publish_group
CREATE TABLE IF NOT EXISTS "publish_group" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(20) NOT NULL);
sqlite> .schema publish_user
CREATE TABLE IF NOT EXISTS "publish_user" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(20) NOT NULL);
?
?
例:
class Blog(models.Model):
??? name = models.CharField(max_length=100)
??? tagline = models.TextField()
?
??? def __str__(self):
??????? return self.name
?
class Author(models.Model):
??? name = models.CharField(max_length=50)
??? email = models.EmailField()
?
??? def __str__(self):
??????? return self.name
?
class Entry(models.Model):
??? blog = models.ForeignKey(Blog)
??? headline = models.CharField(max_length=255)
??? body_text = models.TextField()
??? pub_date = models.DateField()
??? authors = models.ManyToManyField(Author)
??? n_comments = models.IntegerField()
??? n_pingbacks = models.IntegerField()
??? rating = models.IntegerField()
?
??? def __str__(self):
??????? return self.headline
?
例:
?
?
與filter相反:
>>> b = Blog(name='beatles blog', tagline='all the latest beatles news')
>>> b.save()
>>> a1 = Author.objects.create(name='jowin',email='jowin@ane56.com')
>>> a2 = Author.objects.create(name='mage',email='mage@ane56.com')
>>> Author.objects.exclude(name='jowin')
<QuerySet [<Author: mage>]>
?
?
>>> e = Entry(blog=Blog.objects.get(id=1),headline='test',body_text='test',pub_date=timezone.now(),n_comments=20,n_pingbacks=50,rating=100)
>>> e.save()
>>> e = Entry.objects.get(id=1)
>>> e.blog?? #一對多,前向查詢,通過屬性訪問關聯的(外部)對象
<Blog: beatles blog>
>>> e.authors.set(Author.objects.all())
>>> e.authors.all()
<QuerySet [<Author: jowin>, <Author: mage>, <Author: jowin>]>
?
>>> b = Blog.objects.get(id=1)
>>> b.entry_set.all()?? #一對多,反向查詢,模型中有fk,該fk所指的模型實例可通過一個管理器返回前一個模型的所有實例,默認這個管理器的名字是<field-name>_set,<field-name>是源模型的小寫名稱,該管理器返回的查詢集可用get、filter、exclude再次操作
<QuerySet [<Entry: test>]>
>>> b.entry_set.filter(headline__contains='test')
<QuerySet [<Entry: test>]>
>>> b.entry_set.count()
1
?
?
class Entry(models.Model):
???????? blog = models.ForeignKey(Blog, related_name='entries')
使用時用entries_set;
?
?
filter中的關鍵字參數默認是AND的關系;
若用OR的關系,得用Q對象(from django.db.models import Q),Q對象用于封裝一組關鍵字參數;
查詢函數get、filter、exclude中可混合使用Q對象和關鍵字參數,所有提供給查詢函數的參數都將AND在一起,注意Q對象要在關鍵字參數前,如Poll.objects.get(Q(question__startswith='who'),Q(pub_date=date(2019,1,8)|Q(pub_date=date(2019,1,2)),Poll.objects.get(Q()|Q(),question__startswith='who');
推薦用符號&和|;
>>> from django.db.models import Q
>>> Author.objects.filter(Q(name='jowin')|Q(name='mage'))
<QuerySet [<Author: jowin>, <Author: mage>, <Author: jowin>]>
?
?
>>> e = Entry.objects.create(blog=b,headline='test2',body_text='test2',pub_date=timezone.now(),n_comments=30,n_pingbacks=60,rating=90)
>>> e.blog
<Blog: test2>
>>> e.blog = b
>>> a3 = Author.objects.create(name='mage2',email='mage2@magedu.com')
>>> a4 = Author.objects.create(name='jowin2',email='jowin2@magedu.com')
>>> e.authors = [a3]
>>> e.save()
>>> e.authors.add(a3,a4)?? #e.authors.set([])清空
>>> e.save()
>>> e.authors.create(name='mage3',email='mage3@magedu.com')?? #查詢創建fk的value
<Author: mage3>
?
?
Entry.objects.filter(headline__startswith='what').exclude(pub_date__gte=datetime.date.today()).filter(pub_date__gte=datetime(2019,1,8))
?
?
使用<field-name>__lookup,使用雙下劃線來查詢;
gt、gte、lt、lte
contains?? #包含
exact?? #精確匹配,默認
startswith?? #開始于
endswith?? #結束于
regex??? #RE
icontains、iexact、istartswith、iendwith、iregex?? #忽略大小寫
in?? #在列表中
range?? #范圍之內
date、year、month、day?? #時間日期類型
?
>>> Entry.objects.filter(pub_date__gt='2018-12-31')
<QuerySet [<Entry: test>, <Entry: test2>]>
>>> Blog.objects.get(name__iexact='beatles blog')
<Blog: beatles blog>
>>> Blog.objects.filter(pk__in=[0,2,4])
<QuerySet [<Blog: test2>]>
>>> Entry.objects.filter(pub_date__range=(datetime.date(2018,12,31),datetime.date(2019,1,9)))
<QuerySet [<Entry: test>, <Entry: test2>]>
>>> Entry.objects.get(body_text__regex=r'^test2')?? #
<Entry: test2>
?
?
>>> Entry.objects.filter(blog__name='beatles blog')
<QuerySet [<Entry: test>]>
>>> Blog.objects.filter(entry__headline__contains='test')?? #反向
<QuerySet [<Blog: beatles blog>, <Blog: test2>]>
>>> Blog.objects.filter(entry__authors__name__isnull=True)?? #多層
<QuerySet []>
>>> Blog.objects.filter(entry__authors__isnull=False,entry__authors__name__isnull=True)?? #多個過濾條件
<QuerySet []>
?
>>> from django.db.models import F?? #模型字段查詢F,通常是將模型字段與常量比較,或比較2個字段值
>>> Entry.objects.filter(n_pingbacks__gt=F('n_comments'))?? #點贊數大于評論數
<QuerySet [<Entry: test>, <Entry: test2>]>
>>> Entry.objects.filter(rating__gt=F('n_comments') + F('n_pingbacks'))
<QuerySet [<Entry: test>]>
?
?
>>> Entry.objects.all()[:3]?? #同limit 5
<QuerySet [<Entry: test>, <Entry: test2>]>
>>> Entry.objects.all()[1:2]?? #同offset 1 limit 1
<QuerySet [<Entry: test2>]>
?
?
查詢對象比較,用==,在后臺比較的是2個模型主鍵的值:
some_entry == other_entry?? #some_entry.id == other_entry.id
?
?
https://docs.djangoproject.com/en/2.1/ref/models/querysets/
?
annotate():
添加注釋屬性,與聚合函數一起使用,返回聚合值;
>>> from blog.models import Blog,Author,Entry
>>> from django.db.models import Count
>>> q = Blog.objects.annotate(Count('entry'))
>>> q
<QuerySet [<Blog: beatles blog>, <Blog: test2>]>
>>> q[0].name
'beatles blog'
>>> q[0].entry__count
1
>>> qs = Blog.objects.annotate(entry_num=Count('entry'))
>>> for q in qs:
...???? print(q.entry_num)
...
1
1
?
?
aggregate():
聚合函數,返回是字典;
>>> qs = Blog.objects.aggregate(Count('entry'))
>>> qs
{'entry__count': 2}
>>> qs = Blog.objects.aggregate(entry_num=Count('entry'))
>>> qs
{'entry_num': 2}
?
?
聚合類函數:
from django.db.models import Avg,Count,Max,Min,StdDev(標準除),Sum,Variance(方差)
聚合類函數配合聚合函數aggregate()或注釋函數annotate()使用;
?
?
distinct():
去重;
>>> Author.objects.distinct()
<QuerySet [<Author: jowin>, <Author: mage>, <Author: jowin>, <Author: mage2>, <Author: jowin2>, <Author: mage3>]>
>>> Entry.objects.order_by('blog')
<QuerySet [<Entry: test>, <Entry: test2>]>
>>> Entry.objects.order_by('pub_date')
<QuerySet [<Entry: test>, <Entry: test2>]>
?
?
values():
返回是字典(列表套字典),而不是模型實例對象;
>>> Blog.objects.filter(name__startswith='beatles')
<QuerySet [<Blog: beatles blog>]>
>>> Blog.objects.filter(name__startswith='beatles').values()
<QuerySet [{'name': 'beatles blog', 'id': 1, 'tagline': 'all the latest beatles news'}]>
>>> Blog.objects.values()
<QuerySet [{'name': 'beatles blog', 'id': 1, 'tagline': 'all the latest beatles news'}, {'name': 'test2', 'id': 2, 'tagline': 'test2'}]>
>>> Blog.objects.values('id','name')?? #可指定顯示某些字段
<QuerySet [{'name': 'beatles blog', 'id': 1}, {'name': 'test2', 'id': 2}]>
?
?
values_list():
返回列表套元組;
>>> Entry.objects.values_list('id')
<QuerySet [(1,), (2,)]>
>>> Entry.objects.values_list('id').order_by('id')
<QuerySet [(1,), (2,)]>
>>> Entry.objects.values_list('id',flat=True)?? #可指定顯示某些字段;flat扁平,若元組中僅1個元素用flat會將元組脫掉,結果為列表
<QuerySet [1, 2]>
>>> Entry.objects.values_list('id',flat=True).order_by('id')
<QuerySet [1, 2]>
?
?
defer()、only():
若模型包含一些含有大量數據的類型,通常模型會將數據庫取出的數據轉換為py對象,有時可不需要浪費這樣的性能,用defer排隊、only僅轉換指定的(將指定的字段轉為可用屬性方式訪問);
>>> Entry.objects.defer('headline')
<QuerySet [<Entry: test>, <Entry: test2>]>
>>> Entry.objects.only('headline')
<QuerySet [<Entry: test>, <Entry: test2>]>
?
?
using():
使用哪個數據庫;
在一主多從情況下,將查詢指定到從庫上,在settings.py中配;
DATABASES = {
??? 'default': {
??????? 'ENGINE': 'django.db.backends.sqlite3',
??????? 'NAME': os.path.join(BASE_DIR, 'db0.sqlite3'),
??? },
??? 'backup': {...},
}
>>> Entry.objects.all()
<QuerySet [<Entry: test>, <Entry: test2>]>
>>> Entry.objects.using('default')?? #Entry.objects.using('backup')
<QuerySet [<Entry: test>, <Entry: test2>]>
?
?
select_for_update():
返回一個queryset,會鎖定相關行直到事務結束,在支持的數據庫上產生一個select ... for update語句;
>>> Entry.objects.select_for_update().filter(authors=Author.objects.get(id=1))
<QuerySet [<Entry: test>]>
?
?
raw():
執行原始sql;
>>> for s in Blog.objects.raw('select * from blog_blog'):
...???? print(s)
...
beatles blog
test2
?
?
get_or_create()、update_or_create():
能查到,返回;查不到,創建;
obj, created = Person.objects.get_or_create(
???????? first_name='John',
???????? last_name='Lennon',
???????? defaults={'birthday':date(2019,1,8)},
)
等價于
try:
???????? obj = Person.objects.get(first_name='John', last_name='Lennon')
except Person.DoesNotExist:
???????? obj = Person(first_name='John',last_name='Lennon',birthday=date(2019,1,8))
???????? obj.save()
?
?
bulk_create():
批量創建;
>>> Blog.objects.bulk_create([Blog(name='test3',tagline='test3'),Blog(name='test4',tagline='test4')])
[<Blog: test3>, <Blog: test4>]
?
?
in_bulk():
將主鍵封裝成列表,返回指定主鍵的記錄;
>>> Blog.objects.in_bulk([3,4])
{3: <Blog: test3>, 4: <Blog: test4>}
>>> Blog.objects.in_bulk([1,3])
{1: <Blog: beatles blog>, 3: <Blog: test3>}
?
?
latest()、earliest、first()、last():
>>> Entry.objects.latest('pub_date')
<Entry: test>
>>> Entry.objects.earliest('pub_date')
<Entry: test>
?
?
get_lastest_by()經常用,在Meta中指定;
?
?
事務是在view中實現,在函數執行完后才統一commit,默認是autocommit;
例:
from django.db import transaction
?
@transaction.atomic
def my_view(request):
??? do_stuff()
???
@transaction.atomic
def viewfunc(request):
??? do_stuff()
?
?
?
?
1、添加額外管理器,是為類增加“表級”功能的首選方式,可返回你想要的任何數據,而不是返回一個查詢集;
如果要添加行級功能,如只對某個模型的實例起作用,應使用模型方法,而不是管理器方法;
?
?
?
2、添加自定義管理器:
from django.utils.translation import ugettext as _
?
class AuthorManager(models.Manager):
??? def get_queryset(self):
??????? return super(AuthorManager, self).get_queryset().filter(role='A')
?
class EditorManager(models.Model):
??? def get_queryset(self):
??????? return super(EditorManager, self).get_queryset().filter(role='E')
?
class Person(models.Model):
??? first_name = models.CharField(max_length=50)
??? last_name = models.CharField(max_length=50)
??? role = models.CharField(max_length=1, choices=(('A', _('Author'), ('E',_('Editor')))))
??? people = models.Manager()
??? authors = AuthorManager()
??? editors = EditorManager()
?
?
?
?
?
?
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。