我有3张桌子:
class Specialist(models.Model) :
rating = models.FloatField(default=0.0)
class Service(models.Model) :
parent_id = models.IntegerField(null=True, blank=True)
class SpecialistPrice(models.Model) :
specialist = models.ForeignKey(Specialist, related_name='price')
service = models.ForeignKey(Service, related_name='service')
price_bottom = models.PositiveSmallIntegerField(null=True, blank=True)
我需要按SpecialistPrice.price_bottom
<{1}}以及SpecialistPrice.service.parent_id=0
和第2排序条件-rating
我需要一个建议。
UPD 我做了:
queryset = queryset.annotate(min_price=Min('price__price_bottom')).order_by('min_price', '-rating')
但得到了错误
Cannot resolve keyword 'min_price' into field.
原始sql工作正常。
SELECT `specialist`.`id`, `specialist`.`rating`,MIN(`specialistprice`.`price_bottom`) AS `min_price` FROM `specialist` LEFT OUTER JOIN `specialistprice` ON ( `specialist`.`id` = `specialistprice`.`specialist_id` ) GROUP BY `specialist`.`id` ORDER BY `min_price` ASC, `rating` DESC;
答案 0 :(得分:0)
您应该阅读lookups that span relationships。 此查询应该适合您:
Specialist.objects.filter(price__service__parent_id=0).order_by('price', '-rating')