1. Django Tutorial(Airbnb) - Meta Class

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

Django

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

๐ŸŒˆ Meta Class

๐Ÿ”ฅ abstract

๐Ÿ”ฅ verbose_name & verbose_name_plural

๐Ÿ”ฅ ordering


1. abstract

  • Meta Class์˜ abstract๋Š” Default๊ฐ€ "abstract=False"์ด๊ณ , "abstract=True"๋กœ ํ•  ๊ฒฝ์šฐ Abstract Model๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ํ•ด์š”. ์‹ค์ œ ์กด์žฌํ•˜๋Š” DB๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ Model์—์„œ ์ƒ์†ํ•˜์—ฌ ์‚ฌ์šฉํ•  ๋ชฉ์ ์œผ๋กœ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
class AbstractItem(core_models.TimeStampedModel): # ๐Ÿ‘ˆ TimeStampedModel ์ƒ์† ๋ฐ›์Œ
    """AbstractItem Object Definition"""
    name = models.CharField(max_length=80)
    class Meta:
        abstract = True # ๐Ÿ‘ˆ Abstract Model๋กœ ์‚ฌ์šฉ
    def __str__(self):
        return self.name
class RoomType(AbstractItem): # ๐Ÿ‘ˆ AbstractItem ์ƒ์† ๋ฐ›์Œ
    """Room Type Model Definition"""
    pass
class Amenity(AbstractItem): # ๐Ÿ‘ˆ AbstractItem ์ƒ์† ๋ฐ›์Œ
    """Amenity Model Definition"""
    pass
class Facility(AbstractItem): # ๐Ÿ‘ˆ AbstractItem ์ƒ์† ๋ฐ›์Œ
    """Facility Model Definition"""
    pass
class HouseRule(AbstractItem): # ๐Ÿ‘ˆ AbstractItem ์ƒ์† ๋ฐ›์Œ
    """HouseRule Model Definition"""
    pass 

2. verbose_name & verbose_name_plural

  • Admin Panel์„ ๋ณด๋ฉด, Django์—์„œ ์•Œ์•„์„œ ๋ชจ๋“  Model ์ด๋ฆ„์— "s"๋ฅผ ๋ถ™์—ฌ์ค€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”. Django๊ฐ€ ์•Œ์•„์„œ ๋ณต์ˆ˜๋กœ ํ‘œํ˜„์‹œ์ผœ ์ค๋‹ˆ๋‹ค.
  • ๋‹ค๋งŒ, "verbose_name"๊ณผ "verbose_name_plural" ์†์„ฑ์„ ํ†ตํ•ด ์ด๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์–ด์š”!
    • verbose_name์€ "์ง€์ •ํ•œ ๊ฐ’ + s"๋ฅผ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค.
    • verbose_name_plural์€ "์ง€์ •ํ•œ ์ด๋ฆ„" ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
class RoomType(AbstractItem):
    """Room Type Model Definition"""
    class Meta:
        verbose_name = "Room Type" # ๐Ÿ‘ˆ Room Types๋กœ ๋‚˜ํƒ€๋‚จ
class Amenity(AbstractItem):
    """Amenity Model Definition"""
    class Meta:
        verbose_name_plural = "Amenities" # ๐Ÿ‘ˆ Amenities๋กœ ๋‚˜ํƒ€๋‚จ
class Facility(AbstractItem):
    """Facility Model Definition"""
    class Meta:
        verbose_name_plural = "Facilities" # ๐Ÿ‘ˆ Facilities๋กœ ๋‚˜ํƒ€๋‚จ
class HouseRule(AbstractItem):
    """HouseRule Model Definition"""
    class Meta:
        verbose_name = "House Rule" # ๐Ÿ‘ˆ House Rules๋กœ ๋‚˜ํƒ€๋‚จ

3. ordering

  • ordering์€ Model์˜ ๋ฐ์ดํ„ฐ(Object)๋“ค์„ ์ •๋ ฌํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•ด์š”!
    • ๐Ÿ”Ž ordering = [์ •๋ ฌ ๊ธฐ์ค€ ํ•„๋“œ๊ฐ’]
class RoomType(AbstractItem):
    """Room Type Model Definition"""
    class Meta:
        verbose_name = "Room Type"
        ordering = [created] # ๐Ÿ‘ˆ ์ƒ์„ฑ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ(๋ถ€๋ชจ Model์— created ํ•„๋“œ๊ฐ€ ์กด์žฌ)
class Amenity(AbstractItem):
    """Amenity Model Definition"""
    class Meta:
        verbose_name_plural = "Amenities"
        ordering = [name] # ๐Ÿ‘ˆ ์•ŒํŒŒ๋ฒณ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ(๋ถ€๋ชจ Model์— name ํ•„๋“œ๊ฐ€ ์กด์žฌ)

profile
Keep Going, Keep Coding!

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