๐ฅ Abstract Model ์ด๋?
๐ฅ Abstract Model ์์๋ฐ๊ธฐ
๐ฅ Django Country ๋ผ์ด๋ธ๋ฌ๋ฆฌ
1) core/models.py
- ์ด์ ์ฝ๋๋ก๋ง ์กด์ฌํ๋ ์ถ์์ ์ธ Model Class์ด๋ ์๋ฏธ๋ก Abstract Model์ด๋ผ ๋ถ๋ฅด๊ณ , DB์ ์กด์ฌํ์ง ์๋ ๊ฐ์์ Model๋ก ๊ธฐ๋ฅํ๊ธฐ ์ํด class Meta์ "abstract = True"๋ฅผ ์ค์ ํด ์ค๋๋ค.
from django.db import models # Create your models here. class TimeStampedModel(models.Model): # ๐ ๋ค๋ฅธ ๋ชจ๋ธ์์ ์ฌ์ฉ๋์ด์ง Abstract Model์ ๋๋ค:) """Time Stamped Definition""" created = models.DateField(auto_now_add=True) # ๐ ๋ค๋ฅธ ๋ชจ๋ธ์์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉํ ํ๋ updated = models.DateField(auto_now=True) # ๐ ๋ค๋ฅธ ๋ชจ๋ธ์์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉํ ํ๋ class Meta: abstract = True # ๐ Abstract Model๋ก ์ฌ์ฉํ๊ธฐ ์ํด์๋ abstract=True๋ฅผ ์ง์ ํด์ค์ผํด์!
1) rooms/models.py
from django.db import models from core import models as core_models # Create your models here. class Room(core_models.TimeStampedModel): # ๐ TimeStampedModel์ ํ๋๋ฅผ ์์ ๋ฐ์ ์ฌ์ฉ """Room Model Definifion""" pass
1) Django country ๋?
- CharField์ choices ์์ฑ์ผ๋ก ๋ชจ๋ ๊ตญ๊ฐ๋ฅผ ์์๋ก ๋ง๋ค์ด ๊ตญ๊ฐ ๋ชฉ๋ก์ ์ ๊ณตํ๋ ๊ฒ๋ ๋ฌผ๋ก ๊ฐ๋ฅํฉ๋๋ค. ๋ค๋ง, ๋ชจ๋ ๊ตญ๊ฐ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ฃผ๋ Django country๋ฅผ ์ฌ์ฉํ๋ฉด ํธ๋ฆฌํด์! ์ธ์ฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ ์ค์น๊ฐ ํ์ํฉ๋๋ค.
- Django Country ์ค์น
- ๐ pipenv install django-countries
2) settings.py
- Django Country๋ ์ธ๋ถ์์ ์ค์นํ์ฌ ์ฌ์ฉ๋๋ app๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ด๊ธฐ ๋๋ฌธ์ settings.py์์ App๋ฑ๋ก ์์ผ์ค๋๋ค! ์ด๋ด ๊ฒฝ์ฐ๋ฅผ ๋๋นํด ๋ง๋ THIRD_PARTY_APPS์ ๋ฃ์ด์ค๋๋ค.
# Application definition DJANGO_APPS = [ "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", ] PROJECT_APPS = [ "users.apps.UsersConfig", "rooms.apps.RoomsConfig", "reviews.apps.ReviewsConfig", "reservations.apps.ReservationsConfig", "lists.apps.ListsConfig", "core.apps.CoreConfig", "conversations.apps.ConversationsConfig", ] THIRD_PARTY_APPS = [ "django_countries", # ๐ Django Country App ๋ฑ๋ก ] INSTALLED_APPS = DJANGO_APPS + PROJECT_APPS + THIRD_PARTY_APPS
3) rooms/models.py
- Django Country๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด import ํฉ๋๋ค.
- ๐ from django_countries.fields import CountryField
- Django Country์ ํ๋ ์ถ๊ฐ๋ ์๋์ ๊ฐ์ด ๊ฐ๋จํฉ๋๋ค. Admin Panel์ ๋ํ๋ ์ ์๋๋ก list_display์ country๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๊ตญ๊ฐ ๋ชฉ๋ก์ด ๋ํ๋ฉ๋๋ค!
from django.db import models from django_countries.fields import CountryField # ๐ Django Country ๊ฐ์ ธ์ค๊ธฐ from core import models as core_models # Create your models here. class Room(core_models.TimeStampedModel): """Room Model Definifion""" name = models.CharField(max_length=140) description = models.TextField() country = CountryField() # ๐ ๋.