2017-05-22 6 views
0

Whoosh 검색 결과에서 결과를 어떻게 만들 수 있습니까? JSON serializable을 사용하면 해당 데이터를 클라이언트에 반환 할 수 있습니까?Whoosh 검색 결과가 Json serializable이 아님

붕 검색 출력 (파이썬 객체 목록) :이 일을

[<Hit {'content': 'This is the second example.', 'path': '/b', 'icon': '/icons/sheep.png', 'title': 'Second try'}>, <Hit {'content': 'Examples are many second.', 'path': '/c', 'icon': '/icons/book.png', 'title': "Third time's the charm"}>] 

오류 : 나는 별도의 클래스

class DataSerializer(serializers.Serializer): 

    icon=serializers.CharField() 
    content=serializers.CharField() 
    path=serializers.CharField() 
    title=serializers.CharField() 

있지만 오류를 만드는 시도

return JsonReponse({"data": whoosh_results}) 



TypeError: <Hit {'content': 'This is the second example.', 'path': '/b', 'icon': '/icons/sheep.png', 'title': 'Second try'}> is not JSON serializable 

히트 객체에 'icon'속성이 없음이됩니다.

+0

검색하는 데 사용한 코드를 표시 할 수 있습니까? – zaidfazil

+2

''dict'에서 래핑을 시도 했습니까? 'return JsonReponse ({ "data": dict (whoosh_results)})와 비슷합니다. – Igonato

답변

1

당신이 dictwhoos_results 포장 당신이 그 (것)들에게 JSON serializable을 할 수있는 경우 @Igonato이 지적 하듯이 :

return JsonReponse({"content": response['content'], 'path': response['path']}) 

행운을 빕니다 : 심지어 밖으로 당신의 사전의 개별 부품을 취할 수

response = dict(whoosh_results) 
return JsonReponse({"data": response) 

:)

+0

배열을 반복하고 각 파이썬 객체를 조작하여 작동하도록해야했습니다. 감사! – user3226932

+0

No prob mate :) –