python - Django Queryset sort by order_by with relatedManager -


i tryint objects sorted. code:

ratings = rate.objects.order_by(sortid) locations = location.objects.filter(locations_rate__in=ratings).order_by('locations_rate').distinct('id') 

this 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 locations in order equal of ratings?

what 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 field use sorting rate objects, example:

ratings = rate.objects.all() locations = location.objects.filter(     locations_rate__in=ratings ).order_by('locations_rate__%s' % sortid).distinct('id') 

you not need sort ratings beforehand.

the documentation provides example of use of order_by on related fields.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -