실전 Django의 모델 조작의 selectrelated 및 prefetchrelated 모범 사례 (3)
여기
1편은 여기서 예와 select 을 강의합니다.related()
2편은 여기서 프리페치related()
4. 일부 인스턴스
함수 선택
만약에 우리가 모든 고향이 호북인 사람을 얻고 싶다면 가장 머리 없는 방법은 먼저 호북성을 얻은 다음에 호북성의 모든 도시를 얻고 마지막에 고향이 이 도시의 사람을 얻는 것이다.다음과 같이 하십시오.
-
>>> hb = Province.objects.get(name__iexact=
u" ")
-
>>> people = []
-
>>>
for city
in hb.city_set.all():
-
... people.extend(city.birth.all())
-
...
, 1+( ) SQL 。 , 。
prefetch_related() , 。
-
>>> hb = Province.objects.prefetch_related(
"city_set__birth").objects.get(name__iexact=
u" ")
-
>>> people = []
-
>>>
for city
in hb.city_set.all():
-
... people.extend(city.birth.all())
-
...
2 prefetch, 3 SQL :
-
SELECT
`QSOptimize_province`.
`id`,
`QSOptimize_province`.
`name`
-
FROM
`QSOptimize_province`
-
WHERE
`QSOptimize_province`.
`name`
LIKE
' ' ;
-
-
SELECT
`QSOptimize_city`.
`id`,
`QSOptimize_city`.
`name`,
`QSOptimize_city`.
`province_id`
-
FROM
`QSOptimize_city`
-
WHERE
`QSOptimize_city`.
`province_id`
IN (
1);
-
-
SELECT
`QSOptimize_person`.
`id`,
`QSOptimize_person`.
`firstname`,
`QSOptimize_person`.
`lastname`,
-
`QSOptimize_person`.
`hometown_id`,
`QSOptimize_person`.
`living_id`
-
FROM
`QSOptimize_person`
-
WHERE
`QSOptimize_person`.
`hometown_id`
IN (
1,
3);
... , 3 ? ?
>>> people = list(Person.objects.select_related("hometown__province").filter(hometown__province__name__iexact=u" "))
-
SELECT
`QSOptimize_person`.
`id`,
`QSOptimize_person`.
`firstname`,
`QSOptimize_person`.
`lastname`,
-
`QSOptimize_person`.
`hometown_id`,
`QSOptimize_person`.
`living_id`,
`QSOptimize_city`.
`id`,
-
`QSOptimize_city`.
`name`,
`QSOptimize_city`.
`province_id`,
`QSOptimize_province`.
`id`,
`QSOptimize_province`.
`name`
-
FROM
`QSOptimize_person`
-
INNER
JOIN
`QSOptimize_city`
ON (
`QSOptimize_person`.
`hometown_id` =
`QSOptimize_city`.
`id`)
-
INNER
JOIN
`QSOptimize_province`
ON (
`QSOptimize_city`.
`province_id` =
`QSOptimize_province`.
`id`)
-
WHERE
`QSOptimize_province`.
`name`
LIKE
' ';
+----+-----------+----------+-------------+-----------+----+--------+-------------+----+--------+
| id | firstname | lastname | hometown_id | living_id | id | name | province_id | id | name | +----+-----------+----------+-------------+-----------+----+--------+-------------+----+--------+ | 1 | | | 3 | 1 | 3 | | 1 | 1 | |
| 2 | | | 1 | 3 | 1 | | 1 | 1 | | | 3 | | | 3 | 2 | 3 | | 1 | 1 | |
+----+-----------+----------+-------------+-----------+----+--------+-------------+----+--------+
3 rows in set (0.00 sec)
。 SQL ,python 。
select_related() prefetch_related()。 , select_related() , , ForeignKey , prefetch_related()。
QuerySet, 。
model:Order ( )
-
class Order(models.Model):
-
customer = models.ForeignKey(Person)
-
orderinfo = models.CharField(max_length=
50)
-
time = models.DateTimeField(auto_now_add =
True)
-
def __unicode__(self):
-
return self.orderinfo
id 。 ManyToManyField prefetch_related()。 prefetch_related() ?
-
>>> plist = Order.objects.prefetch_related(
'customer__visitation__province').get(id=
1)
-
>>>
for city
in plist.customer.visitation.all():
-
...
print city.province.name
-
...
, 4 :Order、Person、City、Province, prefetch_related() 4 SQL
-
SELECT
`QSOptimize_order`.
`id`,
`QSOptimize_order`.
`customer_id`,
`QSOptimize_order`.
`orderinfo`,
`QSOptimize_order`.
`time`
-
FROM
`QSOptimize_order`
-
WHERE
`QSOptimize_order`.
`id` =
1 ;
-
-
SELECT
`QSOptimize_person`.
`id`,
`QSOptimize_person`.
`firstname`,
`QSOptimize_person`.
`lastname`,
`QSOptimize_person`.
`hometown_id`,
`QSOptimize_person`.
`living_id`
-
FROM
`QSOptimize_person`
-
WHERE
`QSOptimize_person`.
`id`
IN (
1);
-
-
SELECT (
`QSOptimize_person_visitation`.
`person_id`)
AS
`_prefetch_related_val`,
`QSOptimize_city`.
`id`,
-
`QSOptimize_city`.
`name`,
`QSOptimize_city`.
`province_id`
-
FROM
`QSOptimize_city`
-
INNER
JOIN
`QSOptimize_person_visitation`
ON (
`QSOptimize_city`.
`id` =
`QSOptimize_person_visitation`.
`city_id`)
-
WHERE
`QSOptimize_person_visitation`.
`person_id`
IN (
1);
-
-
SELECT
`QSOptimize_province`.
`id`,
`QSOptimize_province`.
`name`
-
FROM
`QSOptimize_province`
-
WHERE
`QSOptimize_province`.
`id`
IN (
1,
2);
+----+-------------+---------------+---------------------+
| id | customer_id | orderinfo | time | +----+-------------+---------------+---------------------+ | 1 | 1 | Info of Order | 2014-08-10 17:05:48 |
+----+-------------+---------------+---------------------+
1 row in set (0.00 sec)
±—±----------±---------±------------±----------+
| id | firstname | lastname | hometown_id | living_id |
±—±----------±---------±------------±----------+
| 1 | | | 3 | 1 |
±—±----------±---------±------------±----------+
1 row in set (0.00 sec)
±----------------------±—±-------±------------+
| _prefetch_related_val | id | name | province_id |
±----------------------±—±-------±------------+
| 1 | 1 | | 1 |
| 1 | 2 | | 2 |
| 1 | 3 | | 1 |
±----------------------±—±-------±------------+
3 rows in set (0.00 sec)
±—±-------+
| id | name |
±—±-------+
| 1 | |
| 2 | |
±—±-------+
2 rows in set (0.00 sec)
select_related() prefetch_related(), select_related()
-
>>> plist = Order.objects.select_related(
'customer').prefetch_related(
'customer__visitation__province').get(id=
1)
-
>>>
for city
in plist.customer.visitation.all():
-
...
print city.province.name
-
...
3 SQL ,Django select_related, prefetch_related , 1 SQL :
-
SELECT
`QSOptimize_order`.
`id`,
`QSOptimize_order`.
`customer_id`,
`QSOptimize_order`.
`orderinfo`,
-
`QSOptimize_order`.
`time`,
`QSOptimize_person`.
`id`,
`QSOptimize_person`.
`firstname`,
-
`QSOptimize_person`.
`lastname`,
`QSOptimize_person`.
`hometown_id`,
`QSOptimize_person`.
`living_id`
-
FROM
`QSOptimize_order`
-
INNER
JOIN
`QSOptimize_person`
ON (
`QSOptimize_order`.
`customer_id` =
`QSOptimize_person`.
`id`)
-
WHERE
`QSOptimize_order`.
`id` =
1 ;
-
-
SELECT (
`QSOptimize_person_visitation`.
`person_id`)
AS
`_prefetch_related_val`,
`QSOptimize_city`.
`id`,
-
`QSOptimize_city`.
`name`,
`QSOptimize_city`.
`province_id`
-
FROM
`QSOptimize_city`
-
INNER
JOIN
`QSOptimize_person_visitation`
ON (
`QSOptimize_city`.
`id` =
`QSOptimize_person_visitation`.
`city_id`)
-
WHERE
`QSOptimize_person_visitation`.
`person_id`
IN (
1);
-
-
SELECT
`QSOptimize_province`.
`id`,
`QSOptimize_province`.
`name`
-
FROM
`QSOptimize_province`
-
WHERE
`QSOptimize_province`.
`id`
IN (
1,
2);
+----+-------------+---------------+---------------------+----+-----------+----------+-------------+-----------+
| id | customer_id | orderinfo | time | id | firstname | lastname | hometown_id | living_id |
+----+-------------+---------------+---------------------+----+-----------+----------+-------------+-----------+
| 1 | 1 | Info of Order | 2014-08-10 17:05:48 | 1 | | | 3 | 1 |
+----+-------------+---------------+---------------------+----+-----------+----------+-------------+-----------+
1 row in set (0.00 sec)
±----------------------±—±-------±------------+
| _prefetch_related_val | id | name | province_id |
±----------------------±—±-------±------------+
| 1 | 1 | | 1 |
| 1 | 2 | | 2 |
| 1 | 3 | | 1 |
±----------------------±—±-------±------------+
3 rows in set (0.00 sec)
±—±-------+
| id | name |
±—±-------+
| 1 | |
| 2 | |
±—±-------+
2 rows in set (0.00 sec)
, prefetch_related select_related, Django : select_related, prefetch_related。 prefetch_related ,select_related 。
- select_related() SQL , prefetch_related() SQL , select_related() 。
- , select_related() 。 select_related() prefetch_related()。
- QuerySet select_related() prefetch_related(), SQL 。
- prefetch_related() select_related() , 。
, 。 , , 。 , 。
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Django 라우팅 계층 URLconf 작용 및 원리 해석URL 구성(URLconf)은 Django가 지원하는 웹 사이트의 디렉토리와 같습니다.그것의 본질은 URL과 이 URL을 호출할 보기 함수 사이의 맵표입니다. 위의 예제에서는 URL의 값을 캡처하고 위치 매개 변수로...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.