๋ฐ์ดํฐ๋ฒ ์ด์ค ์ข ๋ฅ
1. one - to - many
ํ๊ฐ์ ๊ธ์ ๋ง์ ์ฌ์ฉ์๊ฐ ์์ฑ์ ํ ์ ์๊ณ ,
ํ ๋ช ์ ์ฌ์ฉ์๊ฐ ์ฌ๋ฌ ๊ฐ์ ๊ธ์ ์์ฑํ ์ ์๋ค.
์ด๋ฌํ ๊ด๊ณ๊ฐ one-to-many ๋ค!
class UserModel(AbstractUser):
class Meta:
db_table = "my_user"
bio = models.TextField(max_length=500, blank=True)
class TweetModel(models.Model):
class Meta:
db_table = "tweet"
author = models.ForeignKey(UserModel, on_delete=models.CASCADE)
content = models.CharField(max_length=256)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class TweetComment(models.Model):
class Meta:
db_table = "comment"
tweet = models.ForeignKey(TweetModel, on_delete=models.CASCADE)
author = models.ForeignKey(UserModel, on_delete=models.CASCADE)
comment = models.CharField(max_length=256)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
์ ์ ์์ฑํ๋ ์ฝ๋์ค ForeignKey๋ฅผ ๋ณด๋ฉด one-to-many์ธ๊ฑธ ์ ์ ์๋ค.
2. one - to - one
class UserModel(AbstractUser):
...(์๋ต)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_pk = models.IntegerField(blank=True)
nickname = models.CharField(max_length=200, blank=True)
point = models.IntegerField(default=0)
phone = models.CharField(max_length=200, blank=True)
์ด ์ฝ๋๋ฅผ ๋ณด๋ฉด UserModel์ AbstractUser์ ๊ธฐ๋ฅ์ ๋ฐ์์์ ์ฌ์ฉํ๊ธฐ๋๋ฌธ์ username, email,, ๋ฑ๋ฑ ๋ง๋ค!
Profile ํด๋์ค๋ User๋ชจ๋ธ์ ํ๋์ ์ ๋ณด๋ก ๊ฐ์ง๊ณ ์๋ค.
์์ OneToOneField ๋ผ๋ ๊ฒ์ ์ด ์ ์ ๋ชจ๋ธ๊ณผ ํ๋กํ์ด ์ผ๋์ผ๋ก ๋งค์นญ๋๋ค๋ ๊ฒ์ด๋ค.
ํ๋์ ์ฌ์ฉ์ ๋น ํ๋กํ์ ํ๋๋ง ๊ฐ์ง ์ ์๋ค!
3. many - to - many
A๋ชจ๋ธ๊ณผ B๋ชจ๋ธ์ด ์์ ๋ A๊ฐ ์ฌ๋ฌ ๊ฐ์ B๋ฅผ ๊ฐ์ง ์๋ ์๊ณ B๊ฐ ์ฌ๋ฌ ๊ฐ์ A๋ฅผ ๊ฐ์ง ์๋ ์๋ ๊ฒฝ์ฐ!
class MyTopping(models.Model):
topping_name = models.CharField(max_length=100)
class MyPizza(models.Model):
pizza_name = models.CharField(max_length=100)
pizza_topping = models.ManyToManyField(MyTopping)
ํผ์๋ ์ฌ๋ฌ๊ฐ์ ํ ํ์ ๊ฐ์ง ์ ์๊ณ
์ฌ๋ฆฌ๋ธ๋ ์ฌ๋ฌ๊ฐ์ง ํผ์์ ๋ค์ด๊ฐ ์ ์๋ค! ์ด๋ฐ ๊ด๊ณ์ด๋ค!
์ฐ์ต ์ฑ ์์ฑํ๊ธฐ
1. ์ฑ ๋ง๋ค๊ธฐ
ํฐ๋ฏธ๋ ์ฐฝ์
django-admin startapp restaurant ์ ๋ ฅํ๋ฉด ์ข์ธก ํ์๊ธฐ์ ํด๋ ์๊ธด๋ค
2. ํ ์คํธ ์ฑ ์ฐ๊ฒฐ (์ฅ๊ณ ํํ ์๋ ค์ฃผ๊ธฐ)
myspartasns์ settings.py์
INSTALLED_APPS์ 'restaurant', ์ถ๊ฐํด์ฃผ๊ธฐ
Many -to - Many ๋ชจ๋ธ ๋ฑ๋กํ๊ธฐ
1. ๋ชจ๋ธ ๋ง๋ค๊ธฐ
restaurant/models.py์ ๋ชจ๋ธ ๋ง๋ค๊ธฐ
# restaurant/models.py
from django.db import models
# Create your models here.
class MyTopping(models.Model):
class Meta:
db_table = "my_topping"
def __str__(self):
return self.topping_name
topping_name = models.CharField(max_length=100)
class MyPizza(models.Model):
class Meta:
db_table = "my_pizza"
def __str__(self):
return self.pizza_name
pizza_name = models.CharField(max_length=100)
pizza_topping = models.ManyToManyField(MyTopping)
2. db์ ์ ์ฉ์์ผ์ฃผ์
python manage.py makemigrations
python manage.py migrate
3. admin์ ๋ฑ๋กํ๊ธฐ
restaurant/admin.py์ ์๋ ์ฝ๋ ์ ๋ ฅ
from django.contrib import admin
from .models import MyTopping, MyPizza
# Register your models here.
admin.site.register(MyPizza)
admin.site.register(MyTopping)
4. ๊ด๋ฆฌ์๊ณ์ ๋ง๋ค๊ธฐ
admin์ ๋ก๊ทธ์ธํ ๊ด๋ฆฌ์๊ณ์ ์ ๋ง๋ค์ด์ฃผ์
python manage.py createsuperuser
์ฐจ๋ก๋๋ก username, email, password, password(again) ์ ๋ ฅํด์ฃผ๊ณ ์์ฑํ๋ค!
5. http://127.0.0.1:8000/admin/ ๋ก ์ ์ํ๊ธฐ
์์ฑํ ๊ด๋ฆฌ์ ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธ์ ํ๋ฉด
์ ํ๋ฉด์ด ์์ฃผ ์ ๋์จ๋ค
ํ ํ์ ์น์ฆ๋ฅผ ์ถ๊ฐ์ํค๊ณ ์ My toppings๋ฅผ ๋๋ฅด๊ณ ์น์ฆ ์ถ๊ฐ ํ๋๋
์ด๋ฐ ์ค๋ฅ๊ฐ...... ์ผ๋จ ์๋ฒ ์ข ๋ฃํ๊ณ
์ ์ ๋ชจ๋ธ ํ ์ด๋ธ์ ์ง๊ธ ๋ง๋ ๋ชจ๋ธ๋ก ๋ฐ๊พธ๋ฉด์ ๊ด๋ฆฌ์์ ๊ด๋ จ๋ ์ ๋ณด๋ค์ด ์ ๋๋ก ์ธํ ์ด ๋์ด์์ง ์๊ธฐ๋๋ฌธ!(์ ์ ๊ถํ์ด ์๋ชป๋์ด์)
๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ด๋ จ๋๊ฑธ ์น ์ง์ฐ๊ณ ๋ค์ ํด์ฃผ์
db.sqlite3 ์ญ์
restaurant/migrations, user/migrations, tweet/migrations ์์ __init__.py ๋นผ๊ณ ๋๋จธ์ง (0001, 0002 ์ด๋ฐ๊ฑฐ) ์น ์ญ์
์๋ ์ฝ๋ ํฐ๋ฏธ๋์ ํ ์ค์ฉ์ ๋ ฅํด์ ๋ค์ ์์ฑํด์ฃผ์
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
์๋ฒ๋ฅผ ๋๋ ค์ฃผ๊ณ
http://127.0.0.1:8000/admin
์ฌ๊ธฐ๋ก ๋ค์ด๊ฐ์ ๋ค์ admin ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํด์ฃผ๊ณ
ํ ํ ์ถ๊ฐํ๋ฉด ์ ๋ค์ด๊ฐ์๋ค!
db์๋ ์์ฃผ ์ ๋ค์ด๊ฐ์๋ค
'๐๐ช > Django' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
4์ฃผ์ฐจ_3๋ฒ์งธ_์น๊ตฌ ๋ฆฌ์คํธ ๋ง๋ค๊ธฐ (0) | 2022.09.25 |
---|---|
4์ฃผ์ฐจ_2๋ฒ์งธ django shell (0) | 2022.09.24 |
3์ฃผ์ฐจ_6๋ฒ์งธ_๊ฒ์๊ธ ์ฝ๊ธฐ/์ญ์ (1) | 2022.09.23 |
3์ฃผ์ฐจ_5๋ฒ์งธ_๊ฒ์๊ธ ์ฐ๊ธฐ (0) | 2022.09.23 |
3์ฃผ์ฐจ_4๋ฒ์งธ_๋ก๊ทธ์ธ ํ์ ๊ธฐ๋ฅ, ๋ก๊ทธ์์ ๋ง๋ค๊ธฐ (1) | 2022.09.23 |
๋๊ธ