2014-08-29 2 views
1

여기에 내 생각이다 :<장고 개체>는 : 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}) 

idclandpin이 외래 키가 아니라 ssectionid가.

그래서 외래 키를 직렬화하는 방법은 무엇입니까?

답변

1

당신은이 같은 시리얼 클래스를 사용할 수 있습니다 :

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 및 landproperty 모델 클래스의 속성 fieldname2 있습니다.

landproperty 클래스에 대한 사용자 정의 serializer를 작성하고 render를 호출 할 때 사용할 수 있습니다.

+0

감사! 나는 그것을 시도했지만 에러를 반환했다. 'str'객체에 'transform'속성이 없다. –

+0

query_agao가 쿼리 개체 (단일 모델 개체가 아님)인지 확인할 수있다. 게시 할 수 있다면 도움이 될 것이다. 스택 추적 – Shivansh

관련 문제