ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Django] 어드민 페이지 디테일 수정하기
    파이썬/Django 2023. 1. 4. 14:53
    728x90
    반응형

    생성한 어메니티가 클래스 명으로 나오는 현상 고치기

    어메니티 클래스 내부에 아래 함수를 추가한다.

        def __str__(self) -> str:
            return self.name

     

    전체코드

    📁 rooms/models.py

    from django.db import models
    from common.models import CommonModel
    
    class Room(CommonModel):
        """Room Model Definition"""
        
        class RoomKindChoices(models.TextChoices):
            ENTIRE_PLACE = ("entire_place", "Entire Place")
            PRIVATE_ROOM = ("private_room", "Private Room")
            SHARED_ROOM = ("shared_room", "Shared Room")
    
        name = models.CharField(
            max_length=180,
            default="",
        )        
        country = models.CharField(max_length=50, default="한국")
        city = models.CharField(max_length=80, default="서울")
        price = models.PositiveIntegerField()
        rooms = models.PositiveIntegerField()
        toilets = models.PositiveIntegerField()
        description = models.TextField()
        address = models.CharField(max_length=250,)
        pet_friendly = models.BooleanField(default=True)
        kind = models.CharField(max_length=20, choices=RoomKindChoices.choices,)
        owner = models.ForeignKey(
            "users.User",
            on_delete=models.CASCADE,
            related_name="rooms",
        )
        amenities = models.ManyToManyField(
            "rooms.Amenity",
            related_name="rooms",
        )
        
        category = models.ForeignKey(
            "categories.Category",
            null=True,
            blank=True,
            on_delete=models.SET_NULL,
            related_name="rooms",
        )
        
        def __str__(self) -> str:
            return self.name
        
        # 방법1 models.py에 작성
        def total_amenities(room):
            return room.amenities.count()
        
        def rating(room):
            count = room.reviews.count()
            if count == 0:
                return "리뷰 없음"
            else:
                total_rating = 0
                for review in room.reviews.all().values("rating"):
                    total_rating += review["rating"]
                return round(total_rating / count, 2)
        
    class Amenity(CommonModel):
        """ Amenity Definition """
        
        name = models.CharField(max_length=150,)
        description = models.CharField(max_length=150, null=True, blank=True)
        
        def __str__(self) -> str:
            return self.name
        
        class Meta:
            verbose_name_plural = "Amenities"

     

    이상하게 자동완성된 복수형태 수정해주기

    Amenities 가 올바른 표기이지만 s만 붙은 상태로 나와있다.

    이를 고쳐줄텐데 Amenity 클래스 내부에 아래 코드를 작성해준다.

     

    class Meta:
        verbose_name_plural = "Amenities"

     

     

    app 이름 변경해주기

    apps.py에서 변경해준다

     

    📁 direct_messages/apps.py

    class DirectMessagesConfig(AppConfig):
        default_auto_field = 'django.db.models.BigAutoField'
        name = 'direct_messages'
        verbose_name = "Direct Messages"
    LIST
Designed by Tistory.