Django Queryset幾個不常見的api

一、related_name
django還有一種經過對象訪問相關聯表數據的方法,即用_set。可是這種方法只能是相關類訪問定義了關係的類(主鍵類訪問外鍵類)。mysql

class Blog(models.Model):
    pass

class Entry(models.Model):
    blog = models.ForeignKey(Blog)

如:b.entry_set.all() #b是一個blog對象sql

另外還有就是若是Entry中blog屬性的定義若是改爲這樣:blog=models.ForeignKey(Blog,related_name='entries')這樣的話就能夠像下面這樣經過blog對象獲得entry對象的一個集合:數據庫


b=Blog.objects.get(id=1) b.entries.all() #==b.entry_set.all()# b.entries is a Manager that returns QuerySets. b.entries.filter(headline__contains='Lennon') b.entries.count()

二、values和values_list
返回的相似列表和字典的形式django

Blog.objects.values()
[{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}],
Blog.objects.values('id', 'name')
[{'id': 1, 'name': 'Beatles Blog'}]

三、selected_related()
使用selected_related()的時候會自動的查詢相關的外鍵的關係,這樣可能會減小數據庫查詢的次數。併發

#查詢數據庫
e = Entry.objects.get(id=5)
#會再次查詢數據庫
b = e.blog

#查詢數據庫
e = Entry.objects.select_related().get(id=5)
#不會查詢數據庫了
b = e.blog

參考
https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.select_relatedoracle

四、select_for_update
在部分大併發的時候,咱們更新數據以前要先鎖住記錄,select_for_update就能夠作到這個。post

All matched entries will be locked until the end of the transaction block, meaning that other transactions will be prevented from changing or acquiring locks on them.

Usually, if another transaction has already acquired a lock on one of the selected rows, the query will block until the lock is released. If this is not the behavior you want, call select_for_update(nowait=True). This will make the call non-blocking. If a conflicting lock is already acquired by another transaction, DatabaseError will be raised when the queryset is evaluated.

Currently, the postgresql_psycopg2, oracle, and mysql database backends support select_for_update(). However, MySQL has no support for the nowait argument. Obviously, users of external third-party backends should check with their backend’s documentation for specifics in those cases.

Passing nowait=True to select_for_update() using database backends that do not support nowait, such as MySQL, will cause a DatabaseError to be raised. This is in order to prevent code unexpectedly blocking.

五、bulk_create
這個能夠在一次數據庫查詢的時候建立多條記錄ui

Entry.objects.bulk_create([
     Entry(headline="Django 1.0 Released"),
     Entry(headline="Django 1.1 Announced"),
     Entry(headline="Breaking: Django is awesome")
])

六、this

gt
Greater than.

Example:

Entry.objects.filter(id__gt=4)
SQL equivalent:

SELECT ... WHERE id > 4;
gte
Greater than or equal to.

lt
Less than.

lte
Less than or equal to.

七、注意在使用Date查詢DateTimeField的range的時候,不包括最後一天的。例如lua

import datetime
start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
Entry.objects.filter(pub_date__range=(start_date, end_date))

這時候會生成這樣的sql語句
SELECT ... WHERE pub_date BETWEEN '2005-01-01' and '2005-03-31';
若是裏面的start_date等是DateTimeField的時候,就會產生這樣的sql語句
SELECT ... WHERE pub_date BETWEEN '2005-01-01 00:00:00' and '2005-03-31 00:00:00';

相關文章
相關標籤/搜索