<Django Object>: is not serializable JSON

Here is my view:

 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 and clandpin are not foreign, but ssectionid .

So how to serialize foreign keys?

+1
source share
1 answer

You can use the serializers class as follows:

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

If you want to serialize multiple fields, do the following:

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

where fieldname1 and fieldname2 are attributes of the landproperty model class.

Alternatively, you can write your own serializer for your landproperty class and use it when rendering is called.

+1
source

Source: https://habr.com/ru/post/1415064/


All Articles