【问题标题】:Model design for restaurant, meal and meal category餐厅、膳食和膳食类别的模型设计
【发布时间】:2017-01-10 05:54:45
【问题描述】:

我正计划开发一款本地餐馆的送餐应用。我正在考虑最好的设计。我也为 API 建模设计了一个 json。但是我对菜单部分感到困惑。应该在餐厅用餐作为外键或餐厅在用餐中作为外键。

我的应用程序的简单概念是

一家餐厅准备各种餐点,为不同类型的顾客提供服务。一顿饭与类别相关联,例如一顿饭可以是蔬菜,也可以是非蔬菜。客户(用户)也可以点饮料。

我的模型设计和api设计适合这种场景吗?

class Restaurant(models.Model):
    name = models.CharField()
    slug = models.SlugField()
    owner = models.ForeignKey(User)
    location = models.CharField()
    city = models.CharField()
    features = models.ManyToManyField(FeatureChoice) # dinner, launch, nightlife,
    timing = models.ManyToManyField(TimingChoice) # sunday, monday, tuesday, 
    is_delivery = models.BooleanField(default=True)
    # meal = models.ForeignKey(Meal) main confusion is here


class Meal(models.Model):
    restaurant = models.ForeignKey(User)
    name = models.CharField()
    price = models.FloatField()
    quantity = models.PositiveIntegerField()
    image = models.ImageField()
    rating = models.IntegerField()


class MealCategory(models.Model)
    meal = models.ForeignKey(Meal)
    name = models.CharField()
    slug = models.SlugField()

REST API 的 json 设计

[
        {
            'name':'Kathmandu Fast Food',
            'owner':'Sanskar Shrestha',
            'location':'Koteshwor',
            'city':'Kathmandu',
            'features':[
                {
                    'features':'Breakfast'
                },

                {
                    'features':'Launch'
                },

                {
                    'features':'NightLife'
                },

            ],

            'timings':[
                {
                    'timing':'MOnday'
                },

                {
                    'timing':'Sunday'
                },
            ],

            'is_delivery':'true',
            'menu':[
                {
                    'name':'Chicken BBQ',
                    'price':990,
                    'quantity':10,
                    'image':'localhost:8000/media/.../',
                    'category':{
                        'name':'Non-veg'
                    }
                },

                {
                    'name':'Veg Chowmin',
                    'price':160,
                    'quantity':20,
                    'image':'localhost:8000/media/',
                    'category':
                        {
                            'name':'Veg'
                        }
                }
            ]
        }
]

请分享您的专业知识。

【问题讨论】:

    标签: python json django api django-models


    【解决方案1】:

    Meal 应该将 MealCategory 作为 ForeignKey。我认为Meal是一个应该独立于餐厅的模型。考虑添加另一个模型

    class RestaurantMeal(models.Model):
        restaurant = models.ForeignKey(Restaurant)
        meal = models.ForeignKey(Meal)
    

    存储有关特定餐厅用餐的数据。

    【讨论】:

    • 感谢您的回答。我的 Json 呢?这样的设计好看吗?您认为为了更好的服务,是否应该更多地涵盖餐厅、餐点和餐点类别的关系?
    • 在我看来 JSON 看起来不错,我只会更改一件事:在功能和时间上只返回一个包含功能/时间的列表,而不是字典列表
    • 我应该怎么做才能只返回功能和时间列表?我必须为此使用 Django Rest Framework 吗?你能提供一个提示吗?
    • 我绝对推荐用于 REST API 的 django rest 框架,它有很多对你的应用程序有帮助的功能。在序列化程序中,您可以指定字段类型
    【解决方案2】:
    1. 当你应该有restaurant = models.ForeignKey('Restaurant')时,为什么你在Meal模型中有restaurant = models.ForeignKey(User)
    2. MealCategory应该是独立的,不属于Meal,相反,Meal应该属于MealCategory
    3. price 字段应为 Decimal
    4. 为什么rating 是整数?你见过评级曾经是整数吗?只有一个人评价它才是整数。

      class Meal(models.Model):
          restaurant = models.ForeignKey('Restaurant')
          name = models.CharField()
          price = models.DecimalField()
          quantity = models.PositiveIntegerField()
          image = models.ImageField()
          rating = models.FloatField()
          meal_category = models.ForeignKey('MealCategory')
      
      
      class MealCategory(models.Model):
          name = models.CharField()
          slug = models.SlugField()
      

    【讨论】:

    • 对于第一点,这是一个错字。关于第三点,我打算写 IntegerField 但写了 FloatField。如果价格是美元,十进制会很好,但在尼泊尔货币整数的情况下会起作用。非常感谢您解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2020-03-13
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多