主要优化: - templates/index.html: 220KB → 28KB,内联CSS/JS提取为静态文件 - menu/serializers.py: 修复 Order.ingredients_summary N+1 查询问题 - menu/services/dish_service.py: 移除多余查询,添加 only() 限制字段 - menu/views.py: 上传接口添加每分钟20次限流 - static/js/api.js: 统一API错误处理 - zhangmenu/settings.py: 添加Whitenoise压缩、Redis缓存支持、环境变量配置 - static/css/app.css, static/js/app.js: 提取的内联资源文件 - 新增 migrations, requirements.txt, .env.example Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
27 lines
810 B
Python
27 lines
810 B
Python
from django.db.models import F
|
|
from menu.models import Dish
|
|
|
|
|
|
class DishService:
|
|
"""菜品业务逻辑"""
|
|
|
|
@staticmethod
|
|
def get_queryset(category_id=None, search=None):
|
|
"""获取菜品查询集"""
|
|
queryset = Dish.objects.select_related('category').only(
|
|
'id', 'name', 'category', 'category__name', 'cover_image',
|
|
'description', 'ingredients', 'view_count', 'created_at', 'updated_at'
|
|
)
|
|
|
|
if category_id:
|
|
queryset = queryset.filter(category_id=category_id)
|
|
if search:
|
|
queryset = queryset.filter(name__icontains=search)
|
|
|
|
return queryset
|
|
|
|
@staticmethod
|
|
def increment_view_count(dish):
|
|
"""增加浏览次数"""
|
|
Dish.objects.filter(pk=dish.pk).update(view_count=F('view_count') + 1)
|