문제

I am tryint to get objects sorted. 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?

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:

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.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top