1. Django Tutorial(Airbnb) - Abstract Model

ID์งฑ์žฌยท2021๋…„ 7์›” 30์ผ
0

Django

๋ชฉ๋ก ๋ณด๊ธฐ
12/43
post-thumbnail

๐ŸŒˆ Abstract Model

๐Ÿ”ฅ Abstract Model ์ด๋ž€?

๐Ÿ”ฅ Abstract Model ์ƒ์†๋ฐ›๊ธฐ

๐Ÿ”ฅ Django Country ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ


1. Abstract Model ์ด๋ž€?

  • Abstract Model๋Š” ์—ฌ๋Ÿฌ Model์— ๊ณตํ†ต๋œ field๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ ํ•  ๋•Œ, ์ฝ”๋“œ๋ฅผ ์ค‘๋ณต์„ ์ตœ์†Œํ™”์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ์š”,, ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ํ•„๋“œ๋ฅผ ๋ณ„๋„์˜ Model Class๋กœ ์ƒ์„ฑํ›„ ์ด๋ฅผ ๊ฐ Model์—์„œ ์ƒ์† ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” ๊ฑฐ์—์š”!
  • ๋˜ํ•œ Abstract Model์€ ์ด๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” Model์—์„œ๋งŒ ์‹ค์กดํ•˜๊ณ , Abstract Model ์ž์ฒด๋Š” ์‹ค์ œ๋กœ DB์— ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์ƒ์˜ DB๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

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๋ฅผ ์ง€์ •ํ•ด์ค˜์•ผํ•ด์š”!

2. Abstract Model ์ƒ์†๋ฐ›๊ธฐ

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

3. Django Country ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

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()  # ๐Ÿ‘ˆ ๋.

profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€