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)