Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[검수] 220211 generic relation import 누락 반영 #58

Open
wants to merge 121 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
a80f68e
first commit
leehosung Sep 3, 2018
aa31e26
update index.rst
leehosung Sep 3, 2018
ac0ccd4
Merge pull request #1 from 8percent/180904-first-test
leehosung Sep 3, 2018
4e96357
테스트 목적으로 업데이트
leehosung Sep 4, 2018
f3738ab
order_by 부분 번역
initialkommit Sep 22, 2018
0ca0b15
union 부분 번역
initialkommit Sep 22, 2018
97cfcda
ADD: 1.one_to_on TO 4.self-referencing
anohk Sep 25, 2018
dfd13d4
CHG: modify syntax
anohk Sep 25, 2018
b62b207
CHG: modify syntax
anohk Sep 25, 2018
2d81868
ADD: 번역 추가
anohk Sep 26, 2018
7dfdeb5
modify syntax
anohk Sep 26, 2018
1a128ec
번역 추가
anohk Sep 26, 2018
be525e5
modify syntax
anohk Sep 26, 2018
bd97ba1
Translate Queryset ordering
Kirade Sep 26, 2018
ed47b2e
Translate rest
Kirade Sep 26, 2018
f684954
Update f_query.rst
hshwang-jdlab Sep 26, 2018
2b10a64
Update filefield.rst
hshwang-jdlab Sep 26, 2018
17439a0
Update join.rst
hshwang-jdlab Sep 26, 2018
33b94e3
Update second_largest.rst
hshwang-jdlab Sep 26, 2018
e9ff7d1
Update distinct.rst
hshwang-jdlab Sep 26, 2018
60c9167
Update duplicate.rst
hshwang-jdlab Sep 26, 2018
824f9b0
annotated field에 code block 깨진 거 처리
initialkommit Sep 27, 2018
4e4b472
code block 사이에 whitespace 추가
initialkommit Sep 27, 2018
6f0814c
code block 처리
initialkommit Sep 27, 2018
7ca4e29
code block 처리
initialkommit Sep 27, 2018
38885dd
code block 처리
initialkommit Sep 27, 2018
7525409
code block 처리
initialkommit Sep 27, 2018
f8d6c5f
Merge pull request #5 from 8percent/tips
bakyeono Sep 29, 2018
fcda676
Merge pull request #2 from 8percent/TRANSLATE-order-by
bakyeono Sep 29, 2018
940e561
Merge pull request #3 from 8percent/relationship
bakyeono Sep 29, 2018
0e018cc
Merge pull request #4 from 8percent/generic_fk
bakyeono Sep 29, 2018
3d53508
Merge branch 'master' into jaeyoung
bakyeono Sep 29, 2018
463ae43
Merge pull request #6 from 8percent/jaeyoung
bakyeono Sep 29, 2018
49efb6b
Merge pull request #7 from hshwang-jdlab/patch-2
bakyeono Sep 29, 2018
4e8a6a2
Merge pull request #8 from 8percent/TRANSLATE-union
bakyeono Sep 29, 2018
ad174b7
README, index
bakyeono Sep 29, 2018
6930675
introduction
bakyeono Sep 29, 2018
13adc11
query
bakyeono Sep 29, 2018
7ec7618
or_query
bakyeono Sep 29, 2018
c73efa3
and_Query
bakyeono Sep 29, 2018
afbdf39
notequal_query
bakyeono Sep 29, 2018
797fac1
union
bakyeono Sep 29, 2018
43e8358
Django ORM cookbook Chapter 2.14 부터 Chapter 2.17까지 번역.
teekey-dev Sep 29, 2018
ffa5099
select_some_fields
bakyeono Sep 29, 2018
ffbb4d1
subquery
bakyeono Sep 29, 2018
4324bd7
subquery
bakyeono Sep 29, 2018
3fa19af
f_query
bakyeono Sep 29, 2018
e28bf1e
filefield
bakyeono Sep 29, 2018
744cd58
join
bakyeono Sep 29, 2018
5ddeabe
second_largest 1
bakyeono Sep 29, 2018
fe1902f
second_largest 1
bakyeono Sep 29, 2018
f9e80c4
Merge pull request #9 from hegrecom/2_14_to_2_17
bakyeono Sep 29, 2018
58c0981
second_largest 2
bakyeono Sep 29, 2018
3baf91f
duplicate
bakyeono Sep 29, 2018
421180a
distinct
bakyeono Sep 29, 2018
c1d4a16
query_relatedtool
bakyeono Sep 29, 2018
0320bc0
aggregation
bakyeono Sep 29, 2018
1cd4c7f
random
bakyeono Sep 29, 2018
d8e2fe6
func_expressions
bakyeono Sep 29, 2018
60a65f8
Merge branch 'master' of github.com:8percent/django-orm-cookbook-ko
bakyeono Sep 29, 2018
74f46ea
2부까지 퇴고
bakyeono Sep 29, 2018
2923068
introduction
bakyeono Sep 29, 2018
6ed7cc8
오타, 문구 수정
bakyeono Sep 29, 2018
d071946
singleton
bakyeono Sep 29, 2018
b2bc04d
truncate
bakyeono Sep 29, 2018
54ea6bc
signals
bakyeono Sep 29, 2018
ca47733
datetime
bakyeono Sep 29, 2018
e0f1601
fix: truncate
bakyeono Sep 29, 2018
bfb034c
join 원어 병기
bakyeono Sep 30, 2018
7c865a7
auto increment 병기
bakyeono Sep 30, 2018
4ee1165
signals
bakyeono Sep 30, 2018
e2e02d2
copy
bakyeono Sep 30, 2018
382bc9a
distinct
bakyeono Sep 30, 2018
7d234ea
관대한 -> 선한
bakyeono Sep 30, 2018
0d66fa4
asc_or_desc
bakyeono Sep 30, 2018
6fcf661
case_insensitive
bakyeono Sep 30, 2018
1332f03
order_by_related_model
bakyeono Sep 30, 2018
db67599
order_by_related_model
bakyeono Sep 30, 2018
c96fb00
order_by_annotated_field
bakyeono Sep 30, 2018
0fbc17d
one_to_one
bakyeono Sep 30, 2018
9bffffc
one_to_one
bakyeono Sep 30, 2018
6b34927
one_to_many
bakyeono Sep 30, 2018
aa38e06
many_to_many
bakyeono Sep 30, 2018
4920031
self_fk
bakyeono Sep 30, 2018
f38ac94
existing_database
bakyeono Sep 30, 2018
a8fc533
database_view
bakyeono Sep 30, 2018
6bfff1a
Merge pull request #10 from 8percent/퇴고-1부-2부
bakyeono Sep 30, 2018
a980114
Merge branch 'master' into 퇴고-4부-5부
bakyeono Sep 30, 2018
de1387b
generic_models
bakyeono Sep 30, 2018
77bcc9f
generic_models
bakyeono Sep 30, 2018
c978b7e
table_names
bakyeono Sep 30, 2018
c4563ad
column_name
bakyeono Sep 30, 2018
aa233f1
index
bakyeono Sep 30, 2018
68eaccb
null_vs_blank
bakyeono Sep 30, 2018
cc1ac24
uuid
bakyeono Sep 30, 2018
0ebbf2b
slugfield
bakyeono Sep 30, 2018
418388f
multiple_databases
bakyeono Sep 30, 2018
75f5652
numqueries
bakyeono Sep 30, 2018
343a026
keepdb
bakyeono Sep 30, 2018
8931c98
refresh_from_db
bakyeono Sep 30, 2018
9f4da3b
Merge pull request #11 from 8percent/퇴고-4부-5부
bakyeono Sep 30, 2018
b025060
Update README.md
bakyeono Sep 30, 2018
a9b86ae
Update query.rst
hshwang-jdlab Sep 30, 2018
f4f6184
Merge pull request #12 from hshwang-jdlab/patch-3
bakyeono Sep 30, 2018
a842e1f
조판 오류 수정 및 2차 퇴고
bakyeono Sep 30, 2018
faaada9
Merge pull request #13 from 8percent/조판오류수정
bakyeono Sep 30, 2018
9a0fdf5
NEW: PDF 빌드 설정 및 스크립트
leehosung Sep 30, 2018
b3d3151
CHG: 공백 없애기
leehosung Oct 1, 2018
14431b9
CHG: 북커버 제외하기
leehosung Oct 1, 2018
7dfb1c6
FIX: typo
leehosung Oct 1, 2018
118f3df
Merge pull request #15 from 8percent/181001_typo
bakyeono Oct 1, 2018
d2aebad
Merge pull request #14 from 8percent/181001_pdf
bakyeono Oct 1, 2018
02bcf00
Syntax highlighting
mingrammer Oct 2, 2018
5819349
Merge pull request #16 from mingrammer/master
leehosung Oct 2, 2018
a4100a5
FIX: typo
9bow Oct 2, 2018
b4fbbe4
FIX: typo
9bow Oct 2, 2018
702b747
Merge pull request #17 from 9bow/master
bakyeono Oct 3, 2018
bf0d941
FIX: typo
lee-seul Oct 6, 2018
dbbb321
깨진 code block 수정
lee-seul Oct 6, 2018
159ade0
Merge pull request #18 from lee-seul/master
bakyeono Oct 7, 2018
e7ef160
ADD: import 누락 추가
kyungjunleeme Feb 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
subquery
bakyeono committed Sep 29, 2018
commit ffbb4d1763f603d6c872ca2d4d6669d20a622b80
22 changes: 10 additions & 12 deletions docs/subquery.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
How to do a subquery expression in Django?
=============================================
장고에서 서브쿼리 식을 사용하려면 어떻게 하나요?
==============================================================

Django allows using SQL subqueries.
Let's start with something simple, We have a :code:`UserParent` model which has :code:`OnetoOne` relation with auth user. We will find all the :code:`UserParent` which have a :code:`UserParent`.
장고에서 SQL 서브쿼리 식을 사용할 수 있습니다. 간단한 것부터 시작해 봅시다. :code:`auth_user` 모델과 일 대 일(:code:`OneToOne`) 관계로 연결된 :code:`UserParent` 모델이 있다고 합시다. 아래 코드로 ``UserParent`` 모델에서 ``auth_user``를 가진 행을 모두 구할 수 있습니다.

.. code-block:: ipython

@@ -11,9 +10,9 @@ Let's start with something simple, We have a :code:`UserParent` model which has
>>> UserParent.objects.filter(user_id__in=Subquery(users.values('id')))
<QuerySet [<UserParent: UserParent object (2)>, <UserParent: UserParent object (5)>, <UserParent: UserParent object (8)>]>

Now for something more complex. For each :code:`Category`, we want to find the most benevolent :code:`Hero`.
조금 더 까다로운 예제를 살펴봅시다. :code:`Category` 모델의 각 행 별로, 가장 선한 :code:`Hero` 행을 구해 봅시다.

The models look something like this.
모델은 다음과 같이 준비합니다.

.. code-block:: python

@@ -32,7 +31,7 @@ The models look something like this.
)


You can find the most benevolent Hero like this
이 모델에서 가장 관대한 영웅을 구하려면 다음 코드를 실행합니다.

.. code-block:: python

@@ -45,7 +44,7 @@ You can find the most benevolent Hero like this
)
)

If you look at the generated sql, you will see
이 코드가 실행하는 SQL 질의문은 다음과 같습니다.

.. code-block:: sql

@@ -60,17 +59,16 @@ If you look at the generated sql, you will see
FROM "entities_category"


Let's break down the queryset logic. The first part is
질의문을 한 단계씩 나누어 살펴봅시다. 다음 코드가 첫 번쨰 단계입니다.

.. code-block:: python

hero_qs = Hero.objects.filter(
category=OuterRef("pk")
).order_by("-benevolence_factor")

We are ordering the :code:`Hero` object by :code:`benevolence_factor` in DESC order, and using
:code:`category=OuterRef("pk")` to declare that we will be using it in a subquery.
:code:`Hero`의 행들을 선함(:code:`benevolence_factor`)에 따라 내림차순(DESC)으로 정렬하여 선택합니다. 그리고 :code:`category=OuterRef("pk")`를 이용해 이 선택이 서브쿼리로 사용될 수 있도록 준비합니다.

Then we annotate with :code:`most_benevolent_hero=Subquery(hero_qs.values('name')[:1])`, to get use the subquery with a :code:`Category` queryset. The :code:`hero_qs.values('name')[:1]` part picks up the first name from subquery.

그 뒤 :code:`most_benevolent_hero=Subquery(hero_qs.values('name')[:1])`로 서브쿼리에 별칭을 붙여 :code:`Category` 쿼리셋 안에서 사용합니다. 이 때, :code:`hero_qs.values('name')[:1]`는 서브쿼리에서 첫 번째 행의 name 필드를 구하는 코드입니다.