【发布时间】:2016-02-04 00:33:59
【问题描述】:
在以下代码中,如果查询集为空,我想返回一个字符串,其中 results 字段出现在 JSON 响应中。
按原样,我收到一条错误消息object of type 'Response' has no len()。有谁知道在这种情况下如何返回一个字符串?
谢谢!
class ProductListAPIView(CacheMixin, DefaultsMixin, FiltersMixin,
generics.ListAPIView):
serializer_class = ProductSerializer
search_fields = ('title', 'owner',)
ordering_fields = ('created', 'modified', 'list_date_start',)
def get_queryset(self):
products = (Product.objects.select_related('owner')
.prefetch_related('buyers'))
for product in products:
listuse_status_check.send(sender=product)
queryset = products.filter(is_listed=True)
if queryset:
return queryset
return RestResponse({'results': 'some string here.'})
【问题讨论】:
标签: django django-rest-framework django-queryset