在google了很多次之后,我似乎找不到解决这个问题的方法:我创建了一个新的端点,用于从我的产品索引中搜索项目
使用django oscar和django oscar api,以下是模型:
class Product(AbstractProduct):
title = models.TextField(blank = True)
class ProductImage(AbstractProductImage):
product = models.ForeignKey('catalogue.Product', on_delete=models.CASCADE)
display_order = models.PositiveIntegerField()
remote_image = models.URLField(blank=True)
视图.py
class ItemSearchViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
serializer_class = ItemSearchSerializer
def get_queryset(self):
request = self.request
queryset = EmptySearchQuerySet()
if request.GET.get('q', ''):
query = request.GET.get('q', '')
queryset = SearchQuerySet().filter(content=query)
return queryset
序列化程序.py
from oscarapi.serializers import product
class ItemSearchSerializer(serializers.ModelSerializer):
text = serializers.CharField()
title = serializers.CharField()
price = serializers.HyperlinkedIdentityField(view_name='product-price')
image = product.ProductImageSerializer(many=True, required=False)
class Meta():
model = Product
fields = ('image', 'price', 'text', 'title')
当我击中终点时 localhost:8000/api/search/?q=bmw
,它似乎适用于所有其他字段,但不返回图像,以下是示例:
[
{
"image": null,
"price": "http://localhost:8000/api/products/2339/price/",
"text": "2339\nBMW 316 SE Automatic",
"title": "BMW 316 SE Automatic"
},
{
"image": null,
"price": "http://localhost:8000/api/products/0029/price/",
"text": "0029\nBmw 330d\nLoverly car in excellent condition",
"title": "Bmw 330d"
}]
另一件事,因为可能有几个图片为一个给定的产品,我也想只返回第一个图像,这将有一个 display_order
值为1。
暂无答案!
目前还没有任何答案,快来回答吧!