0
我對postgresql(和postgis)有空間查詢。我使用raw()函數。所以我有這樣的事情:如何從sql查詢序列化count()到JSON
observations = Square.objects.raw('SELECT validation_birds_square.id AS id,
validation_birds_square.identification,
Count(validation_birds_checklist.position) AS total
FROM validation_birds_square ...the rest of sql query...')
我使用Square模型,我在我的models.py中定義。然後我想要序列化觀察,所以我做這個:
return HttpResponse(serializers.serialize("json", observations), content_type='application/json')
但是,這是問題所在。它僅從SQL查詢序列化Square模型的屬性,而不是總計。
當我遍歷的意見,我可以得到這個值:
for observation in observations:
print(observation.total)
有沒有什麼更好的辦法如何把總到JSON序列比遍歷觀察和手動序列化到JSON?