forked from erp-dev/erp
fix: current_state has been changed to a computed property
This commit is contained in:
@@ -75,7 +75,8 @@ class ProductQuickAPITestCase(TestCase):
|
||||
for item in response.data['results']:
|
||||
self.assertIn('id', item)
|
||||
self.assertIn('name', item)
|
||||
self.assertEqual(len(item), 2) # 只有 id 和 name 两个字段
|
||||
self.assertIn('image', item) # 新增:验证图片字段
|
||||
self.assertEqual(len(item), 3) # 现在有 id, name 和 image 三个字段
|
||||
|
||||
def test_list_products_with_pagination(self):
|
||||
"""测试获取产品列表 - 带分页"""
|
||||
@@ -150,14 +151,17 @@ class ProductQuickAPITestCase(TestCase):
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# 验证返回的产品只包含 id 和 name
|
||||
# 验证返回的产品包含 id, name 和 image
|
||||
product = response.data['results'][0]
|
||||
self.assertIn('id', product)
|
||||
self.assertIn('name', product)
|
||||
self.assertIn('image', product) # 新增:验证图片字段
|
||||
self.assertIsInstance(product['id'], int)
|
||||
self.assertIsInstance(product['name'], str)
|
||||
# image 可能是 None 或字符串
|
||||
self.assertTrue(product['image'] is None or isinstance(product['image'], str))
|
||||
|
||||
# 确保没有其他字段
|
||||
# 确保没有其他不需要的字段
|
||||
self.assertNotIn('merchant', product)
|
||||
self.assertNotIn('created_at', product)
|
||||
self.assertNotIn('updated_at', product)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user