<;Django对象>;:JSON不可序列化

<;Django对象>;:JSON不可序列化,json,django,Json,Django,以下是我的看法: def display_maps(request): #query_agao = ButuanMaps.objects.filter(clandpin=search_term) #x = Owner.objects.select_related('landproperty_butuanmaps').get(id=5) query_agao = ButuanMaps.objects.all().select_related('landproperty')[:10] query_a

以下是我的看法:

def display_maps(request):
#query_agao = ButuanMaps.objects.filter(clandpin=search_term)
#x = Owner.objects.select_related('landproperty_butuanmaps').get(id=5)
query_agao = ButuanMaps.objects.all().select_related('landproperty')[:10]
query_all = ButuanMaps.objects.all()[:10]
djf = Django.Django(geodjango='geom', properties=['id','clandpin','ssectionid'])
geoj = GeoJSON.GeoJSON()
butuan_agao = geoj.encode(djf.decode(query_agao.transform(3857)))
return render(request, "index.html", {
    'butuan_agao': butuan_agao,
    'query_agao': query_agao,
    'query_all': query_all})
id
clandpin
不是外键,而是
ssectionid


那么,如何序列化外键呢?

您可以像这样使用serializers类:

from django.core import serializers
query_agao = ButuanMaps.objects.all().select_related('landproperty')[:10]
json_serialized_objects = serializers.serialize("json", query_agao)
如果只想序列化几个字段,请执行以下操作:

json_serialized_objects = serializers.serialize("json", query_agao, fields=("fieldname1", "fieldname2"))
其中,fieldname1和fieldname2是landproperty模型类的属性


或者,您可以为landproperty类编写自定义序列化程序,并在调用render时使用它。

谢谢!我试过了,但它返回了一个错误,
'str'对象没有属性“transform”
您可以检查query\u agao是否是queryset对象(它不应该是单个模型对象),如果您可以发布堆栈跟踪,这将很有帮助。