[39 | 提供完了] << [ホーム] >> [41 | UIの更新]
「qrmenu_server」側で作業します。
「qrmenu_server/qrmenucore/models.py」ファイルに記述を追加します。
記述追加 【Desktop/QRMenu/qrmenu_server/qrmenucore/models.py】11,12行目
from django.db import models from django.contrib.auth.models import User from django.utils import timezone # Create your models here. class Place(models.Model): owner = models.ForeignKey(User, on_delete=models.CASCADE) name = models.CharField(max_length=255) image = models.CharField(max_length=255) number_of_tables = models.IntegerField(default=1) font = models.CharField(max_length=100, blank=True) color = models.CharField(max_length=100, blank=True) def __str__(self): return "{}/{}".format(self.owner.username, self.name) class Category(models.Model): place = models.ForeignKey(Place, on_delete=models.CASCADE, related_name="categories") name = models.CharField(max_length=255) def __str__(self): return "{}/{}".format(self.place, self.name) class MenuItem(models.Model): place = models.ForeignKey(Place, on_delete=models.CASCADE) category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="menu_items") name = models.CharField(max_length=255) description = models.TextField(blank=True) price = models.IntegerField(default=0) image = models.CharField(max_length=255) is_available = models.BooleanField(default=True) def __str__(self): return "{}/{}".format(self.category, self.name) class Order(models.Model): PROCESSING_STATUS = "processing" COMPLETED_STATUS = "completed" STATUSES = ( (PROCESSING_STATUS, 'Processing'), (COMPLETED_STATUS, 'Completed'), ) place = models.ForeignKey(Place, on_delete=models.CASCADE) table = models.CharField(max_length=2) detail = models.TextField() payment_intent = models.CharField(max_length=255) amount = models.IntegerField() status = models.CharField(max_length=20, choices=STATUSES, default=PROCESSING_STATUS) created_at = models.DateTimeField(default=timezone.now) def __str__(self): return "{}/{}/{}円".format(self.place, self.table, self.amount)
「qrmenucore/serializers.py」ファイルに記述を追加します。
記述追加 【Desktop/QRMenu/qrmenu_server/qrmenucore/serializers.py】20行目
from rest_framework import serializers from . import models class MenuItemSerializer(serializers.ModelSerializer): class Meta: model = models.MenuItem fields = ( 'id', 'name', 'description', 'price', 'image', 'is_available', 'place', 'category') class CategorySerializer(serializers.ModelSerializer): menu_items = MenuItemSerializer(many=True, read_only=True) class Meta: model = models.Category fields = ( 'id', 'name', 'menu_items', 'place') class PlaceDetailSerializer(serializers.ModelSerializer): categories = CategorySerializer(many=True, read_only=True) class Meta: model = models.Place fields = ( 'id', 'name', 'image', 'font', 'color', 'number_of_tables', 'categories') class PlaceSerializer(serializers.ModelSerializer): class Meta: model = models.Place fields = ('id', 'name', 'image') class OrderSerializer(serializers.ModelSerializer): class Meta: model = models.Order fields = "__all__"
マイグレーションファイルを作成します。
コマンド
python manage.py makemigrations
マイグレーションを実行します。
コマンド
python manage.py migrate
↓↓クリックして頂けると励みになります。
[39 | 提供完了] << [ホーム] >> [41 | UIの更新]