Django Relationship fields (N:1) (Article - User)
User ๋ชจ๋ธ ์ฐธ์กฐ
Django ์์ User ๋ชจ๋ธ์ ์ฐธ์กฐํ๋ ๋ฐฉ๋ฒ
- settings.AUTH_USER_MODEL
- get_user_model()
1. settings.AUTH_USER_MODEL
โ ๋ฐํ๊ฐ: 'accounts.User' (๋ฌธ์์ด)
โ User ๋ชจ๋ธ์ ๋ํ ์ธ๋ ํค ๋๋ M:N ๊ด๊ณ๋ฅผ ์ ์ํ ๋ ์ฌ์ฉ
โ models.py์ ๋ชจ๋ธ ํ๋์์ User ๋ชจ๋ธ์ ์ฐธ์กฐํ ๋ ์ฌ์ฉ
2. get_user_model()
โ ๋ฐํ๊ฐ: User Object(๊ฐ์ฒด)
โ ํ์ฌ ํ์ฑํ๋ User ๋ชจ๋ธ ๋ฐํ
โ models.py ๊ฐ ์๋ ๋ค๋ฅธ ๋ชจ๋ ๊ณณ์์ ์ ์ ๋ชจ๋ธ์ ์ฐธ์กฐํ ๋ ์ฌ์ฉ
๋ชจ๋ธ ๊ด๊ณ ์ค์
![image](https://user-images.githubusercontent.com/109324637/194046646-e71a98fb-ae9a-4649-843c-770824109fb9.png)
1. model.py
![image](https://user-images.githubusercontent.com/109324637/194003283-0c82c368-f51c-46df-8c24-5d815a8283d2.png)
2. makemigrations
![image](https://user-images.githubusercontent.com/109324637/194003704-f0e778d3-83eb-4ba1-a16a-a8b6527c2f29.png)
โ 1 ๋ฒ (๊ธฐ๋ณธ๊ฐ ์ง์ ์ถ๊ฐ)
3. ๊ธฐ๋ณธ๊ฐ ์ถ๊ฐํด์ฃผ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/194003907-fb5f55af-1451-4c85-b221-e4302bb732aa.png)
โ (์์์)๊ธฐ๋ณธ๊ฐ ์ถ๊ฐํด์ฃผ๊ธฐ
4. migrate ์ ์ฉํด์ฃผ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/194004112-5c57b277-f597-4106-8922-6b9d1b8b049c.png)
Create
1. ์ด์ ๊ณผ ์ ์ฌํ๊ฒ ๋ถํ์ํ user field๊ฐ ์ถ๋ ฅ๋๋ ์ํฉ
![image](https://user-images.githubusercontent.com/109324637/194004770-1892ac86-3cf3-49f0-b1f9-3131c85cf20e.png)
2. ArticleForm์ ์ถ๋ ฅ ํ๋ ์์
![image](https://user-images.githubusercontent.com/109324637/194004958-46340f3e-f092-4b79-95ca-2a228f78b98f.png)
3. save์ commit ์ต์ ํ์ฉํด์ ๊ฒ์๊ธ ์์ฑ์ ์ ๋ณด ์ ์ฅ
![image](https://user-images.githubusercontent.com/109324637/194005368-4aaf6ac0-f116-4928-a856-3f7dc2d0cc65.png)
Delete
![image](https://user-images.githubusercontent.com/109324637/194005932-fe230434-4268-4391-b3f6-e6c380093db5.png)
โ ํ์ฌ ์ญ์ ๋ฅผ ์์ฒญํ๋ ค๋ ์ฌ๋๊ณผ ๊ฒ์๊ธ์ ์์ฑํ ์ฌ๋์ ๋น๊ตํ์ฌ ๋ณธ์ธ์ ๊ฒ์๊ธ๋ง ์ญ์ ํ ์ ์๋๋ก ํจ
Update
1. ์์ ๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ ์ ์์ฒญํ๋ ค๋ ์ฌ๋๊ณผ ๊ฒ์๊ธ์ฆ ์์ฑํ ์ฌ๋์ ๋น๊ตํ์ฌ ๋ณธ์ธ์ ๊ฒ์๊ธ๋ง ์์
![image](https://user-images.githubusercontent.com/109324637/194017811-61005afd-f109-4b57-a5fa-da41fa902b3d.png)
2. ์ถ๊ฐ๋ก ํด๋น ๊ธ์ ์์ฑ์๊ฐ ์๋๋ผ๋ฉด, ์์ /์ญ์ ๋ฒํผ ์ถ๋ ฅํ์ง ์๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/194018121-dd4c35a2-5df8-4c55-a2d5-1bbe0a59c4c3.png)
Read
์์ฑ์ ์ถ๋ ฅํ๊ธฐ (index, detail)
![image](https://user-images.githubusercontent.com/109324637/194018573-2cf7bded-ffe9-4f92-ba8c-3d99c5d5071a.png)
โ user.username์ด ์๋๋ผ user๋ง ์ถ๋ ฅํด๋ ์์ฑ์๋ฅผ ๋ฐํํ๋๋ก ๋์ด์์
'โญ 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) Comments - Article Relationship (0) | 2022.10.09 |
๋๊ธ