Files
school-meal/backend/meals/models.py
T
root 65b52b052f 营养校准、后台美化、静态走对象存储 CDN、标题可编辑、admin 拼音搜索
- 菜品营养按食物成分表+烹饪方式校准(calibrate_nutrition 命令)
- 后台接入 jazzmin 主题美化,移除 Google Fonts(模板覆盖)
- 前端/后台静态资源指向 qnycdn 对象存储 CDN(vite base + DJANGO_STATIC_URL)
- 菜单标题学校名称/周数可编辑,日期随周数计算
- admin 搜索支持拼音首字母/全拼(Dish.pinyin 索引,pypinyin)
- Dockerfile 使用清华 pip 镜像源
2026-08-06 21:11:14 +08:00

51 lines
1.9 KiB
Python

from django.db import models
from pypinyin import Style, lazy_pinyin
class Dish(models.Model):
TYPE_CHOICES = [
('meat', '荤菜'),
('veg', '素菜'),
('soup', '汤'),
('staple', '主食'),
]
CUISINE_CHOICES = [
('家常菜', '家常菜'),
('本帮菜', '本帮菜'),
('江浙菜', '江浙菜'),
('川菜', '川菜'),
('粤菜', '粤菜'),
('鲁菜', '鲁菜'),
('湘菜', '湘菜'),
('西北菜', '西北菜'),
('面点小吃', '面点小吃'),
]
name = models.CharField('菜名', max_length=100, unique=True)
dish_type = models.CharField('类型', max_length=10, choices=TYPE_CHOICES)
cuisine = models.CharField('菜系', max_length=20, choices=CUISINE_CHOICES, blank=True, default='家常菜')
ingredient_detail = models.CharField('配料明细', max_length=200, blank=True, help_text='例如:牛心菜85g五花肉片15g')
protein = models.DecimalField('蛋白质(g)', max_digits=6, decimal_places=1, default=0)
fat = models.DecimalField('脂肪(g)', max_digits=6, decimal_places=1, default=0)
calorie = models.IntegerField('热量(kcal)', default=0)
description = models.CharField('备注', max_length=200, blank=True)
is_active = models.BooleanField('启用', default=True)
pinyin = models.CharField('拼音索引', max_length=200, blank=True, editable=False)
created_at = models.DateTimeField('创建时间', auto_now_add=True)
class Meta:
ordering = ['dish_type', 'id']
verbose_name = '菜品'
verbose_name_plural = '菜品'
def __str__(self):
return f'{self.get_dish_type_display()}: {self.name}'
def save(self, *args, **kwargs):
if self.name:
full = ''.join(lazy_pinyin(self.name))
initials = ''.join(lazy_pinyin(self.name, style=Style.FIRST_LETTER))
self.pinyin = f'{full} {initials}'
super().save(*args, **kwargs)