Python Django Queryset使用relatedManager按订单排序
我正试着把物品分类。这是我的代码:Python Django Queryset使用relatedManager按订单排序,python,database,django,django-related-manager,Python,Database,Django,Django Related Manager,我正试着把物品分类。这是我的代码: ratings = Rate.objects.order_by(sortid) locations = Location.objects.filter(locations_rate__in=ratings).order_by('locations_rate').distinct('id') 这是我的模型: class Rate(models.Model): von_location= models.ForeignKey(Location,related_
ratings = Rate.objects.order_by(sortid)
locations = Location.objects.filter(locations_rate__in=ratings).order_by('locations_rate').distinct('id')
这是我的模型:
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)
我如何才能按与评级相同的顺序获取所有位置
我上面所说的不起作用。
编辑:
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))
您必须指定一个字段,用于对速率
对象进行排序,例如:
ratings = Rate.objects.all()
locations = Location.objects.filter(
locations_rate__in=ratings
).order_by('locations_rate__%s' % sortid).distinct('id')
您不需要事先对评级进行排序
提供了在相关字段中使用order\u by
的示例。@PavelAnossov,它是price\u leistung
或bewertung
谢谢,但它是这样说的<代码>视图home.views.sort没有返回HttpResponse对象。
。此查询中出现错误。我认为这是另一个问题:)您的视图需要返回一些数据,如中所述。如果您在这一点上需要更多帮助,您应该发布视图代码。我将此作为ajax响应返回,我将呈现一些html部分,其中包含呈现的数据。所以HttpResponse不应该出现在那里。请在我的更新中查看我的视图在我看来任何事情都是错误的,render\u to\u响应
将返回一个HttpResponse
。我猜请求。在您的视图中,_ajax()
的计算结果是False
,那么,您确定要通过ajax调用访问它吗?另外,即使您的视图只支持AJAX请求,您也应该处理所有类型的响应,例如returnHttpResponseBadRequest
或`对于非AJAX请求,这将有助于调试。我使用Jquery load,我想,这就是原因。