赞
踩
你好,我使用的是django 1.2,然后我想知道如何对原生查询集(RawQuerySet)的行进行计数。
惯例的.count()方法行不通。
这是我的查询
query = "SELECT *, ((ACOS(SIN(%s * PI() / 180) * SIN(lat * PI() / 180) + COS(%s * PI() / 180) * COS(lat * PI() / 180) * COS((%s - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM app_car WHERE price BETWEEN %s AND %s HAVING distance<=%s ORDER BY distance ASC"
cars = Car.objects.raw(query, [lat, lat, lon, min_price, max_price, miles])
return HttpResponse( cars )
然后它的返回值是
Car_Deferred_model_id_user_id object
大家有什么想法吗?
使用’len()'函数,这会带来:
query = "SELECT *, ((ACOS(SIN(%s * PI() / 180) * SIN(lat * PI() / 180) + COS(%s * PI() / 180) * COS(lat * PI() / 180) * COS((%s - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM app_car WHERE price BETWEEN %s AND %s HAVING distance<=%s ORDER BY distance ASC"
cars = Car.objects.raw(query, [lat, lat, lon, min_price, max_price, miles])
return HttpResponse(len(list(cars))
另外:这里有一些关于Django 1.2 Model.objects.raw()方法的有用信息: http://djangoadvent.com/1.2/smoothing-curve/ 【看起来该网站可能已经过期,但Internet Archive将其保存在:http://web.archive.org/web/20110513122309/http://djangoadvent.com/1.2/smoothing-curve/ 】
欢迎来弦圈一起翻译StackOverflow等国外编程内容
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。