from django.contrib import admin from django.utils.html import format_html, mark_safe from .models import Dish @admin.action(description='启用所选菜品(参与自动生成)') def activate_dishes(modeladmin, request, queryset): queryset.update(is_active=True) @admin.action(description='停用所选菜品(不再参与自动生成)') def deactivate_dishes(modeladmin, request, queryset): queryset.update(is_active=False) @admin.register(Dish) class DishAdmin(admin.ModelAdmin): list_display = ('name', 'cuisine', 'dish_type_display', 'ingredient_detail', 'protein', 'fat', 'calorie', 'nutri_badge', 'is_active', 'created_at') list_filter = ('dish_type', 'cuisine', 'is_active') search_fields = ('name', 'ingredient_detail', 'cuisine', 'pinyin') list_editable = ('cuisine', 'ingredient_detail', 'protein', 'fat', 'calorie', 'is_active') list_per_page = 50 actions = [activate_dishes, deactivate_dishes] fieldsets = ( (None, {'fields': ('name', 'dish_type', 'cuisine', 'is_active')}), ('营养信息', {'fields': ('ingredient_detail', 'protein', 'fat', 'calorie')}), ('其他', {'fields': ('description',), 'classes': ('collapse',)}), ) @admin.display(description='营养提示', ordering='calorie') def nutri_badge(self, obj): badges = [] if obj.dish_type == 'meat' and obj.fat >= 20: badges.append('高脂') if obj.dish_type == 'meat' and obj.calorie >= 300: badges.append('高热量') if obj.dish_type in ('meat', 'veg') and obj.protein >= 15: badges.append('高蛋白') if obj.dish_type == 'veg' and obj.fat <= 3 and obj.calorie <= 60: badges.append('清淡') return mark_safe(' '.join(badges)) if badges else '—' @admin.display(description='类型') def dish_type_display(self, obj): colors = {'meat': '#d9534f', 'veg': '#5cb85c', 'soup': '#5bc0de', 'staple': '#f0ad4e'} return format_html( '{}', colors.get(obj.dish_type, '#999'), dict(Dish.TYPE_CHOICES).get(obj.dish_type, obj.dish_type), ) admin.site.site_header = '学校订餐菜单管理系统' admin.site.site_title = '学校订餐菜单管理' admin.site.index_title = '菜单管理后台'