我想篩選我的結果以僅顯示第一個「變體」類指向至「Product」類。Django Rest-篩選結果以顯示第一個變體
我幾乎希望它像這樣工作:
'product__variation__image__image'[0],
OR
'product_set__variation__image__image'[0],
我試着使用:
.distinct('product_pk')
但是,如果我用.order_by這是行不通的()
Th e下面的代碼起作用,但每個項目都會重複幾次,因爲它有與之相關的變化。
這裏是我的Django的休息視圖的片段:在查詢集)
wardrobematch = {
'user': lambda x: ('user__pk', x)
}
class WardrobeListView(APIView):
renderer_classes = (JSONRenderer,)
def get(self, request, *args, **kwargs):
filters = {}
for key, value in request.GET.items():
key = key.lower()
if key in wardrobematch:
lookup, val = wardrobematch[key](value.lower())
filters[lookup] = val
qset = (
Analytic.objects
.filter(like=True,**filters)
# .distinct('product_id',)
.values('product_id', 'product__name', 'product__brand', 'product__store__store_name', 'product__variation__image__image', 'product__variation__price__price', 'updated',)
)
result = sorted(qset, key=lambda obj: obj.updated)
return Response(serializers.serialize("json", result))
你是否在使用sqlite?它不支持「DISTINCT」。要使「DISTINCT」語句正常工作,請切換到MySQL或PostgreSQL。 – RodrigoDela
在PostgreSQL上。剛剛更新的問題 - 可以使用.distinct工作,但是這打破了「order_by」。我需要使用兩者。這是回溯http://dpaste.com/1G7F8YC – Ycon
你的回溯表明你的'distinct()'應該和你的'order_by()'相匹配。你有沒有試過把'product_pk'放到你的'order_by()'中? – RodrigoDela