Django 计数 RawQuerySet [英] Django count RawQuerySet
问题描述
嘿,我正在使用 django 1.2,我想知道如何从原始查询集 (RawQuerySet) 中计算行数.
Hay, I'm using django 1.2 and i want to know how to count rows from a raw queryset(RawQuerySet).
传统的 .count() 方法不起作用.
The traditional .count() method doesn't work.
这是我的查询
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()' 函数.这会给:
Use the 'len()' function. This would give:
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/ [看起来那个网站可能已经过期,但互联网档案馆在:http://web.archive.org/web/20110513122309/http://djangoadvent.com/1.2/smoothing-曲线/ ]
Aside: there's some useful information on the Django 1.2 Model.objects.raw() method at: http://djangoadvent.com/1.2/smoothing-curve/ [Looks like that site might have expired, but the Internet Archive has it at: http://web.archive.org/web/20110513122309/http://djangoadvent.com/1.2/smoothing-curve/ ]
这篇关于Django 计数 RawQuerySet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!