[英]Django Queryset sort by order_by with relatedManager
I am tryint to get objects sorted. 我是tryint让对象排序。 this is my code:
这是我的代码:
ratings = Rate.objects.order_by(sortid)
locations = Location.objects.filter(locations_rate__in=ratings).order_by('locations_rate').distinct('id')
this is my model: 这是我的模特:
class Rate(models.Model):
von_location= models.ForeignKey(Location,related_name="locations_rate")
price_leistung = models.IntegerField(max_length=5,default=00)
bewertung = models.IntegerField(max_length=3,default=00)
how can I get all Locations in that order which is equal to that of ratings
? 如何按顺序获得与
ratings
相同的所有地点?
what I have above isnot working. 我上面所做的不起作用。
EDIT : 编辑 :
def sort(request):
sortid = request.GET.get('sortid')
ratings = Rate.objects.all()
locations = Location.objects.filter(locations_rate__in=ratings).order_by('locations_rate__%s' % sortid).distinct('id')
if request.is_ajax():
template = 'resultpart.html'
return render_to_response(template,{'locs':locations},context_instance=RequestContext(request))
You must specify a field to use for sorting the Rate
objects, for example: 您必须指定用于对
Rate
对象进行排序的字段,例如:
ratings = Rate.objects.all()
locations = Location.objects.filter(
locations_rate__in=ratings
).order_by('locations_rate__%s' % sortid).distinct('id')
You do not need to sort ratings
beforehand. 您无需事先对
ratings
进行排序。
The documentation provides example of use of order_by
on related fields. 该文档提供了在相关字段上使用
order_by
示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.