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.