开发者

How to get objects out of a distinct() in Django?

I wonder if this query can be modified to return User objects instead of just merchant_id (User).

(User.objects.get(id=merc开发者_开发百科hant['merchant_id']) for merchant in self.user.visits.values('merchant_id').distinct())


You are creating 1 DB query for each User (which is a Merchant?) lookup. Need to brush up on my django ORM but I would do something like:

User.objects.filter(id__in=[ merchant['merchant_id'] for merchant in self.user.visits.values('merchant_id').distinct() ])


How does this sound?

merchants_visited = self.user.visits.merchants.all().distinct()
return User.objects.filter(visits__merchants__in=merchants_visited, profile=self)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜