반응형
다대다 관계의 모델을 직접 활용해보자
1. 다대다 관계에서 모델 인스턴스 생성하기
1.객체 생성 후 save() 하기
xxxxxxxxxx
In [4]: person = Person.objects.create(name='권태형')
INSERT INTO "person" ("name")
VALUES ('권태형')
Execution time: 0.002013s [Database: default]
In [5]: python_group = Group.objects.create(name='파이썬모임')
INSERT INTO "group" ("name")
VALUES ('파이썬모임')
Execution time: 0.002968s [Database: default]
In [6]: vegeterian_group = Group.objects.create(name='채식모임')
INSERT INTO "group" ("name")
VALUES ('채식모임')
Execution time: 0.001940s [Database: default]
In [7]: m1 = Membership(person=person, group=python_group, sub_group=vegeterian_group, date_joined='2020-01-01', invite_reaso
...: n="그냥")
In [8]: m1.save()
INSERT INTO "membership" ("person_id", "group_id", "sub_group_id", "date_joined", "invite_reason")
VALUES (2, 3, 4, '2020-01-01', '그냥')
2..create
사용하기
xxxxxxxxxx
In [19]: person_2 = Person.objects.create(name='권태형2')
INSERT INTO "person" ("name")
VALUES ('권태형2')
Execution time: 0.001893s [Database: default]
In [20]: Membership.objects.create(group=vegeterian_group, sub_group=python_group, person=person_2, invite_reason="그냥", dat
...: e_joined='2020-01-01')
INSERT INTO "membership" ("person_id", "group_id", "sub_group_id", "date_joined", "invite_reason")
VALUES (3, 4, 3, '2020-01-01', '그냥')
Execution time: 0.001983s [Database: default]
Out[20]: <Membership: Membership object (2)>
2. through_defaults를 지정하여서, add() 사용하기
xxxxxxxxxx
# python_group이라는 그룹에 멤버를 추가할 때, Person 인스턴스를 넘겨주면 되는데, 이때 멤버쉽의 나머지 필드들은 through_fields에 정의해줄 수 있다
In [32]: python_group.members.add(python_man, through_defaults={'date_joined': '2020-01-01', 'sub_group': vegeterian_group})
BEGIN
Execution time: 0.000040s [Database: default]
SELECT "membership"."person_id"
FROM "membership"
WHERE ("membership"."group_id" = 3 AND "membership"."person_id" IN (4))
Execution time: 0.000108s [Database: default]
INSERT INTO "membership" ("person_id", "group_id", "sub_group_id", "date_joined", "invite_reason") SELECT 4,
3,
4,
'2020-01-01',
''
Execution time: 0.000415s [Database: default]
In [33]: python_group.members.all()
Out[33]: SELECT "person"."id",
"person"."name"
FROM "person"
INNER JOIN "membership"
ON ("person"."id" = "membership"."person_id")
WHERE "membership"."group_id" = 3
LIMIT 21
Execution time: 0.000136s [Database: default]
<QuerySet [<Person: 권태형>, <Person: 파이썬맨>]>
3. remove()
remove를 사용하면 넘어오는 인스턴스에 해당하는 모든 값을 삭제한다.
xxxxxxxxxx
In [39]: python_group.members.all()
Out[39]: SELECT "person"."id",
"person"."name"
FROM "person"
INNER JOIN "membership"
ON ("person"."id" = "membership"."person_id")
WHERE "membership"."group_id" = 3
LIMIT 21
Execution time: 0.000118s [Database: default]
<QuerySet [<Person: 권태형>, <Person: 파이썬맨>, <Person: 권태형2>, <Person: 권태형2>]>
In [40]: person_2
Out[40]: <Person: 권태형2>
In [41]: python_group.members.remove(person_2)
BEGIN
Execution time: 0.000051s [Database: default]
DELETE
FROM "membership"
WHERE ("membership"."group_id" = 3 AND "membership"."person_id" IN (3))
Execution time: 0.000586s [Database: default]
In [42]: python_group.members.all()
Out[42]: SELECT "person"."id",
"person"."name"
FROM "person"
INNER JOIN "membership"
ON ("person"."id" = "membership"."person_id")
WHERE "membership"."group_id" = 3
LIMIT 21
Execution time: 0.000163s [Database: default]
<QuerySet [<Person: 권태형>, <Person: 파이썬맨>]>
4. clear()
깔끔하게 밀고 싶을 땐(?) clear를 사용하자.
xxxxxxxxxx
In [43]: python_group.members.all()
Out[43]: SELECT "person"."id",
"person"."name"
FROM "person"
INNER JOIN "membership"
ON ("person"."id" = "membership"."person_id")
WHERE "membership"."group_id" = 3
LIMIT 21
Execution time: 0.000120s [Database: default]
<QuerySet [<Person: 권태형>, <Person: 파이썬맨>]>
In [44]: python_group.members.clear()
BEGIN
Execution time: 0.000045s [Database: default]
DELETE
FROM "membership"
WHERE "membership"."group_id" = 3
Execution time: 0.000570s [Database: default]
In [45]: python_group.members.all()
Out[45]: SELECT "person"."id",
"person"."name"
FROM "person"
INNER JOIN "membership"
ON ("person"."id" = "membership"."person_id")
WHERE "membership"."group_id" = 3
LIMIT 21
Execution time: 0.000131s [Database: default]
<QuerySet []>
반응형
'개발 > 장고' 카테고리의 다른 글
[해설과 함께 읽는 Django 문서] Models - 다대다 관계에서의 추가 필드 (0) | 2021.01.30 |
---|---|
[Django] 다대다 관계에서 through, through_fields, through_defaults (1) | 2021.01.30 |
[Django] 다대다 관계에서 중개 모델의 제한 사항 (0) | 2021.01.30 |
[해설과 함께 읽는 Django 문서] Models - 다 대 다 관계 (0) | 2021.01.29 |
[해설과 함께 읽는 Django 문서] Models - 다 대 일 관계 (0) | 2021.01.27 |