< Django Object&gt ;:不是JSON可序列化 [英] <Django Object>: is not JSON serializable

查看:135
本文介绍了< Django Object&gt ;:不是JSON可序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的观点:

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

那么如何序列化外键?

推荐答案

您可以使用这样的序列化器类:

You can use serializers class like this :

from django.core import serializers
query_agao = ButuanMaps.objects.all().select_related('landproperty')[:10]
json_serialized_objects = serializers.serialize("json", query_agao)

如果你只想序列化几个字段做这个:

if you only want to serialize few fields do this :

json_serialized_objects = serializers.serialize("json", query_agao, fields=("fieldname1", "fieldname2"))

其中fieldname1和fieldname2是landproperty模型类的属性。

where fieldname1 and fieldname2 are attributes of landproperty model class.

或者,您可以为您的土地职业类编写一个自定义序列化程序,并在调用渲染时使用它。

Alternately you can do a write a custom serializer for your landproperty class and use it at the time of calling render.

这篇关于< Django Object&gt ;:不是JSON可序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆