Django Relationship fields (N:1) (Comments - Article)
Django Relationship fields ์ข ๋ฅ
- OneToOneField() - 1:1
- ForeignKey() - N:1
- ManyToManyField() - N:N
ForeignKey(to, on_delete, **options)
โ N:1 relationship์ ๋ด๋นํ๋ Django์ ๋ชจ๋ธ ํ๋ ํด๋์ค
โ Django ๋ชจ๋ธ์์ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ธ๋ ํค ์์ฑ์ ๋ด๋น
โ 2๊ฐ์ ํ์ ์์น ์ธ์
- ์ฐธ์กฐํ๋ model class
- on_delete์ต์
on_delete
โ ์ธ๋ ํค๊ฐ ์ฐธ์กฐํ๋ ๊ฐ์ฒด๊ฐ ์ฌ๋ผ์ก์ ๋ ์ธ๋ ํค๋ฅผ ๊ฐ์ง ๊ฐ์ฒด๋ฅผ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง๋ฅผ ์ ์
โ ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ์ ์ํด์ ์ค์ํ ์ค์ !
โ ์ต์
๊ฐ
- CASCADE: ๋ถ๋ชจ ๊ฐ์ฒด๊ฐ ์ญ์ ๋์ ๋ ์ด๋ฅผ ์ฐธ์กฐํ๋ ๊ฐ์ฒด๋ ์ญ์
- PROTECT, SET_NULL, SET_DEFAULT ...๋ฑ ์ฌ๋ฌ ์ต์ ์กด์ฌ
Comment ๋ชจ๋ธ
Comment Model ์ ์
![image](https://user-images.githubusercontent.com/109324637/193960205-fffe769e-9177-448d-9937-5ebb7bcc19b1.png)
โ ์ธ๋ ํค ํ๋๋ ForeignKey ํด๋์ค ์์ฑ ์์น์ ๊ด๊ณ ์์ด ํ๋ ๋ง์ง๋ง์ ์์ฑ๋๋ค.
โ ํด๋์ค์ ์ธ์คํด์ค ์ด๋ฆ์ ์ฐธ์กฐํ๋ ๋ชจ๋ธ ํด๋์ค ์ด๋ฆ์ ๋จ์ํ(์๋ฌธ์)๋ก ์์ฑ ๊ถ์ฅ
Migration ๊ณผ์ ์งํ
![image](https://user-images.githubusercontent.com/109324637/193960885-653c1536-77a9-44c9-8e64-9922242cee4d.png)
![image](https://user-images.githubusercontent.com/109324637/193961084-2e998ad7-5b55-4226-9e66-6b310b8b1ff6.png)
โ ๋ณ๋ ์ฌํญ์ด ๋ฐ์ํ์ผ๋ฏ๋ก ๋ค์ migration ์งํ
โ makemigrations -> migrate
![image](https://user-images.githubusercontent.com/109324637/193986688-0460a4d8-2daa-4960-903d-c2bf0e948b0a.png)
โ '[์ค์ ํ ์ธ์คํด์ค ์ด๋ฆ]_id'๋ก ์ธ๋ํค๊ฐ ๋ค์ด๊ฐ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค
๋๊ธ ์์ฑํด๋ณด๊ธฐ
0. shell_plus ์คํ
![image](https://user-images.githubusercontent.com/109324637/193987126-aa5b9226-3371-4633-aab8-9f7301683efe.png)
1. ๊ทธ๋ฅ ์์ฑํ๊ฒ ๋๋ค๋ฉด...
![image](https://user-images.githubusercontent.com/109324637/193987429-22889857-2884-424e-a7d5-1ce0d8c8f83f.png)
![image](https://user-images.githubusercontent.com/109324637/193987519-00cc91c9-d910-4c0b-a527-4d20ba0d8d35.png)
โ ์ฐธ์กฐํ๋ ์ธ๋ํค ๊ฐ์ด ์์ด์ ์๋ฌ ๋ฐ์ (๋๊ธ์ ๋จ๋ ์ผ๋ก ์ธ ์ ์์)
2. Article์ ๋จผ์ ์์ฑํ๊ณ ํด๋น Article์ ๋๊ธ์ ์์ฑ!
![image](https://user-images.githubusercontent.com/109324637/193987800-8931f583-1535-4e3c-91f3-cd04f3960f9f.png)
![image](https://user-images.githubusercontent.com/109324637/193988014-e5e3dde9-6176-407c-8704-f1e86fac2153.png)
โ ์ ์์ ์ผ๋ก ๋๊ธ์ด ์์ฑ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค
(์ฐธ๊ณ ) ๋ฐ๋ก ๊ฐ์ ์ง์ ํด์ฃผ๋ฉด..?
![image](https://user-images.githubusercontent.com/109324637/193988293-ec0eca3f-1543-4c5b-89c7-3bc6eef09bc7.png)
โ ๊ฐ๋ฅ์ ํ๋ ๊ฐ์ฒด ์งํฅ์ ๊ด์ ์์ ๋ฐ๋์งํ ํ๊ธฐ๋ฒ์ ์๋
๋๊ธ ์์ฑ ํ์ธํ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/193988491-030e75e5-7820-47c0-872d-defe04bcf70b.png)
๋๋ฒ์งธ ๋๊ธ ์์ฑํ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/193988649-9129330e-e40e-49a9-adbe-7db3d6b7c0e9.png)
๊ด๊ณ ๋ชจ๋ธ ์ฐธ์กฐ
Related manager
โ N:1 ํน์ M:N๊ด๊ณ์์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฌธ๋งฅ
โ Django๋ ๋ชจ๋ธ ๊ฐ N:1 ํน์ M:N ๊ด๊ณ๊ฐ ์ค์ ๋๋ฉด ์ญ์ฐธ์กฐํ ๋ ์ฌ์ฉํ ์ ์๋ manager ์์ฑ
์ญ์ฐธ์กฐ
โ ๋๋ฅผ ์ฐธ์กฐํ๋ ํ
์ด๋ธ(๋๋ฅผ ์ธ๋ ํค๋ก ์ง์ ํ)์ ์ฐธ์กฐํ๋ ๊ฒ
โ ๋ณธ์ธ์ด ์ธ๋ ํค๋ก ์ฐธ์กฐ ์ค์ธ ๋ค๋ฅธ ํ
์ด๋ธ์ ์ ๊ทผํ๋ ๊ฒ
โ N:1 ๊ด๊ณ์์๋ 1์ด N์ ์ฐธ์กฐํ๋ ์ํฉ
Django์ ๊ด๊ณ ๋ชจ๋ธ ์ฐธ์กฐ
![image](https://user-images.githubusercontent.com/109324637/193989168-fc54c520-4e54-4275-a580-9bd496e439e2.png)
โ article.comment ํ์์ผ๋ก๋ ๋๊ธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.(Article ํด๋์ค ์์ฒด์๋ ์ด๋ ํ ๊ด๊ณ๋ ์์ฑ๋์ด ์์ง ์์)
โ Django๊ฐ ์ญ์ฐธ์กฐํ ์ ์๋ comment_set manager๋ฅผ ์๋์ผ๋ก ์์ฑํด article.comment_set ํํ๋ก ๋๊ธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
โ ๋จ์ ์ฐธ์กฐ(comment -> article)์์๋ comment.article ํํ๋ก ๋ฐ๋ก ์์ฑ ๊ฐ๋ฅ
์์
![image](https://user-images.githubusercontent.com/109324637/193989718-a4edbb0f-bfe8-40fd-8dc1-c3be4c4550bf.png)
โ article 1 ์ ์์ฑ๋ ๋ชจ๋ ๋๊ธ ์กฐํ
![image](https://user-images.githubusercontent.com/109324637/193989939-b9e63b6b-ef0d-4db8-ab37-9bc3af2526a7.png)
โ ์์ ๊ฐ์ด ๋ชจ๋ ๋๊ธ๋ค์ comment ๋ณ์์ ๋ด๊ณ content๋ฅผ ํ๋์ฉ ์ถ๋ ฅํ ์๋ ์๋ค
admin site ๋ฑ๋ก
![image](https://user-images.githubusercontent.com/109324637/193990581-cc5e064b-3164-4a1d-ad5a-b7b60b045363.png)
โ admin site์์ ๋๊ธ์ ํ์ธ/๊ด๋ฆฌํ๊ธฐ ์ํด์๋ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฑ๋ก์ ํด์ค์ผ ํ๋ค.
Comment ๊ตฌํ
Create
1. comment ์ ๋ ฅ์ ์ํ CommentForm ์์ฑ
![image](https://user-images.githubusercontent.com/109324637/193991090-cf2bab66-ed39-475a-a925-3a340e26bc8c.png)
2. detail ํ์ด์ง์ comment ์ถ๊ฐํด์ฃผ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/193991492-259e5e8b-56be-487e-8a69-3e9802e1751e.png)
![image](https://user-images.githubusercontent.com/109324637/193991577-7e1550c0-6be9-4f2b-9fd8-fccddedafdcf.png)
3. ๊ทธ๋ฌ๋...
![image](https://user-images.githubusercontent.com/109324637/193992096-ec2ab475-e10a-4bbc-adca-23f73110b7af.png)
โ ํด๋น ๊ฒ์๊ธ์ ๋๊ธ์ด ๋ฐ๋ก ์์ฑ๋๋๋ก ํ๊ธฐ ์ํด์๋ ์ธ๋ ํค ํ๋๋ฅผ ์ฌ์ฉ์์ ์
๋ ฅ์ด ์๋๋ผ view ํจ์ ๋ด์์ ๋ฐ์ ๋ณ๋๋ก ์ฒ๋ฆฌ๋์ด ์ ์ฅ๋๋๋ก ํด์ผ ํ๋ค.
4. ์ธ๋ ํค ํ๋ ์ถ๋ ฅ์์ ์ ์ธํ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/193992404-baa4c0c1-c0c8-4b93-9b2d-f6b5748656c0.png)
5. ์ถ๋ ฅ์์ ์ ์ธ๋ ์ธ๋ ํค ๋ฐ์์ค๊ธฐ (variable routing)
![image](https://user-images.githubusercontent.com/109324637/193992892-ee4dfa7b-2b8e-4cc3-957b-d33bcd045f47.png)
![image](https://user-images.githubusercontent.com/109324637/193993117-04c9b44c-6d28-48f3-83e8-e0850b1cdee0.png)
![image](https://user-images.githubusercontent.com/109324637/193993692-656afb85-e271-4546-85bf-392ddbd7b3a8.png)
6. ๊ทธ๋ฌ๋...
![image](https://user-images.githubusercontent.com/109324637/193993741-af6483cc-1cc5-48cf-a079-c85e84d31fbc.png)
โ NOT NULL ์๋ฌ ๋ฐ์
7. save ๋ฉ์๋์ commit ์ต์ ์ ์ฌ์ฉํด DB์ ์ ์ฅ๋๊ธฐ ์ article ๊ฐ์ฒด ์ ์ฅ
![image](https://user-images.githubusercontent.com/109324637/193994276-12719193-ce5b-4964-824d-1feb1be30e69.png)
Read
1. ํน์ article์ ์๋ ๋ชจ๋ ๋๊ธ์ ๊ฐ์ ธ์จ ํ context์ ์ถ๊ฐ
![image](https://user-images.githubusercontent.com/109324637/193995650-8d56be6d-459e-4834-aaf8-bc4e277ca772.png)
2. ํ ํ๋ฆฟ์์ ๋๊ธ ๋ชฉ๋ก ์ถ๋ ฅํ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/193995905-cafecbea-531a-41f7-93e5-e7474218fe43.png)
3. ์ ์์ ์ผ๋ก ๋๊ธ์ด ๋ฌ๋ฆฐ ๊ฒ์ ํ์ธํ ์ ์๋ค.
![image](https://user-images.githubusercontent.com/109324637/193995993-9fd81a30-5610-49c4-9397-ab53a91e944f.png)
Delete
1. url ์ถ๊ฐํด์ฃผ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/193996350-9232d1da-d89e-40be-a6f4-3dfd4658b969.png)
2. view ํจ์ ์ถ๊ฐํด์ฃผ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/193996675-55b7e467-62af-4ba6-82f8-5f76c1f56de9.png)
โ ์ญ์ ์ดํ redirectํ๊ธฐ ์ํด์ article_pk ํ์!
3. ์ญ์ ๋ฒํผ ๋ง๋ค์ด์ฃผ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/193997243-80505faf-da5c-4c9f-bb59-623ca31c6d46.png)
๋๊ธ ์์ ์...?
โ ํ์ด์ง์ ์ผ๋ถ ๋ด์ฉ๋ง ์ ๋ฐ์ดํธ ํ๋ ๊ฒ์ JavaScript์ ์์ญ์ด๊ธฐ ๋๋ฌธ์ ์ดํ์ ๋ค์ ์งํ...!!
Comment ๊ด๋ จ ์ถ๊ฐ ์ฌํญ
1. ๋๊ธ ๊ฐ์ ์ถ๋ ฅํ๊ธฐ
- DTL filter - length ์ฌ์ฉ
- Queryset API - count() ์ฌ์ฉ
2. ๋๊ธ์ด ์๋ ๊ฒฝ์ฐ ๋์ฒด ์ปจํ ์ธ ์ถ๋ ฅํ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/193998410-f05660ea-b3e4-40a1-9933-65ffd1daf1da.png)
'โญ Personal_Study > Django' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(M:N) User- User Relationship (0) | 2022.10.18 |
---|---|
(M:N) Article - User Relationship (0) | 2022.10.17 |
Django์์ css, js ๋ฑ static(์ ์ ) ์์ ๋ฑ๋ก/๊ด๋ฆฌํ๊ธฐ (0) | 2022.10.12 |
(N:1) Comment - User Relationship (0) | 2022.10.10 |
(N:1) Article - User Relationship (0) | 2022.10.09 |
๋๊ธ