1
0
forked from erp-dev/erp

fix: current_state has been changed to a computed property

This commit is contained in:
2025-11-19 10:23:17 +08:00
parent 7f822b0064
commit 400c0db6db
14 changed files with 964 additions and 87 deletions

View File

@@ -16,9 +16,14 @@ class ProductQuickViewSet(viewsets.GenericViewSet):
产品快速查询接口
专门为前端下拉框和自动完成提供的轻量级接口
只返回 id 和 name支持分页和模糊搜索
只返回 id, name 和 image支持分页和模糊搜索
性能优化:
- 使用 .only() 只查询需要的字段,避免 SELECT *
- 减少数据传输量
- 提升查询速度
"""
queryset = Product.objects.all()
queryset = Product.objects.only('id', 'name', 'image') # 性能优化:只查询需要的字段
permission_classes = [IsAuthenticated]
pagination_class = LimitOffsetPagination
filter_backends = [DjangoFilterBackend, filters.SearchFilter]
@@ -26,13 +31,18 @@ class ProductQuickViewSet(viewsets.GenericViewSet):
def list(self, request, *args, **kwargs):
"""
获取产品列表(仅 id 和 name
获取产品列表(仅 id, name 和 image
查询参数:
- limit: 返回结果数量,默认无限制
- offset: 偏移量默认0
- search: 按名称模糊搜索
返回字段:
- id: 产品ID
- name: 产品名称
- image: 产品图片URL可能为null
示例:
- GET /api/v1/products/quick/?limit=10&offset=0
- GET /api/v1/products/quick/?search=布料
@@ -42,11 +52,25 @@ class ProductQuickViewSet(viewsets.GenericViewSet):
# 分页
page = self.paginate_queryset(queryset)
if page is not None:
data = [{'id': p.id, 'name': p.name} for p in page]
data = [
{
'id': p.id,
'name': p.name,
'image': p.image.url if p.image else None
}
for p in page
]
return self.get_paginated_response(data)
# 不分页(如果没有 limit 参数)
data = [{'id': p.id, 'name': p.name} for p in queryset]
data = [
{
'id': p.id,
'name': p.name,
'image': p.image.url if p.image else None
}
for p in queryset
]
return Response({
'count': len(data),
'results': data