TIL #34-2 Django-DB 스타벅스

채록·2021년 1월 24일
0

Python & Django

목록 보기
11/34
post-thumbnail

🐶 들어가는 말

스타벅스에 다 적용해보자!!!
단, images 는 CharField로 적용 (images_url)

참고 tool (이미지 넣어도 되는지 확인하고 넣기!)

models.py 작성

class Menu(models.Model):
        name                    = models.CharField(max_length=10)

        def __str__(self):
            return f'{self.name}'

        class Meta:
            db_table = 'Menu'

class Categories(models.Model):
        Menu                    = models.ForeignKey('Menu', on_delete=models.CASCADE)
        name                    = models.CharField(max_length=50)

        def __str__(self):
            return f'{self.name}'

        class Meta:
            db_table = 'Categories'

class Product(models.Model):
        name                    = models.CharField(max_length=50)
        name_en                 = models.CharField(max_length=50, null=True)
        description             = models.CharField(max_length=1000)
#       price                   = models.CharField(max_length=1000, null=True)
#       MENU                    = models.ForeignKey('Menu', on_delete = models.SET_NULL, null=True)
        category                = models.ForeignKey('Categories', on_delete = models.SET_NULL, null=True)
#       allergy_drink           = models.ManyToManyField('Allergy', through='AllergyDrink')
        description             = models.CharField(max_length=100, null=True)
        is_new                  = models.BooleanField(default=False)

        def __str__(self):
                return f'{self.name}'

        class Meta:
                db_table = 'products'

class Nutrition(models.Model):
        product                 = models.OneToOneField('Product', on_delete=models.CASCADE, null=True)
        one_serving_kca         = models.DecimalField(max_digits=6, decimal_places=2, null=True)
        sodium_mg               = models.DecimalField(max_digits=6, decimal_places=2, null=True)
        saturated_fat_g         = models.DecimalField(max_digits=6, decimal_places=2, null=True)
        sugars_g                = models.DecimalField(max_digits=6, decimal_places=2, null=True)
        protein_g               = models.DecimalField(max_digits=6, decimal_places=2, null=True)
        caffeine_mg             = models.DecimalField(max_digits=6, decimal_places=2, null=True)
        size_ml                 = models.CharField(max_length=10, null=True)
        size_fluid_ounce        = models.CharField(max_length=10, null=True)

        class Meta:
                db_table = 'nutritions'

class Allergy(models.Model):
        name                    = models.CharField(max_length=10, null=True)

        def __str__(self):
                return f'{self.name}'

        class Meta:
                db_table = 'allergy'

class AllergyDrink(models.Model):
        allergy                 = models.ForeignKey('Allergy', on_delete=models.CASCADE, null=True)
        product                 = models.ForeignKey('Product', on_delete=models.CASCADE, null=True)

        class Meta:
                db_table = 'allergy_drink'

class Images(models.Model):
        image_url               = models.CharField(max_length=2000, null=True)
        product                 = models.ForeignKey('Product', on_delete=models.CASCADE, null=True)

        class Meta:
                db_table = 'images'

첫번째에 말고 두번째부터 추가된 것은 모두 null=True 해줫다.

table 생성 확인

Django shell : create & update

완성!


^_____^

이미지는 홈페이지 들어가서 url 따와야 하므로 나중에 하기로 했다..

만들기 더 쉬운 방법이 있다면 그걸로 다시.. 해볼꺼야..

profile
🍎 🍊 🍋 🍏 🍇

0개의 댓글