3

ピストン ハンドラーでは、django.db.models.query.QuerySet を適切な JSON メッセージ (基になるモデルとクエリを反映したもの) として返す必要があり、独自の HttpResponse ヘッダーも追加する必要があります。これまでのところ、どちらか一方を行うことはできますが、両方を行うことはできません (適切な外観の JSON 応答を取得します)。

以下は、適切な JSON 形式の応答を生成しますが、HttpResponse ヘッダーが追加されていません (表示されていません)。

class PollHandlerSearch(BaseHandler):
    allowed_methods = ('POST')
    model = Poll
    fields = ('id', 'question', 'pub_date')
    KEYS =  ( 'question', 'start-date', 'end-date' )

    def create(self, request):
        post = Poll.objects.all()
        for skey in self.KEYS:
            if len(post) and request.POST.has_key(skey) and len(request.POST[skey]):
                if skey == self.KEYS[0]:
                        post = post.filter(question__icontains=request.POST[skey])
                elif skey == self.KEYS[1]:
                        post = post.filter(pub_date__gte=request.POST[skey])
                elif skey == self.KEYS[2]:
                        post = post.filter(pub_date__lte=request.POST[skey])
        return post

正しくフォーマットされた JSON メッセージの結果:

[
    {
        "pub_date": "2010-08-23 22:15:07", 
        "question": "What's up?", 
        "id": 1
    }
]

以下は、ヘッダーが追加された HttpResponse を実装し、JSON 風の応答を生成しますが、期待または要求されているものではなく、django の「DateTimeAwareJSONEncoder」が行うこと (ピストンの JSONEmitter によって使用される) を反映していません。

class PollHandlerSearch(BaseHandler):
    allowed_methods = ('POST')
    model = Poll
    fields = ('id', 'question', 'pub_date')
    KEYS =  ( 'question', 'start-date', 'end-date' )

    def create(self, request):
        resp = HttpResponse()
        post = Poll.objects.all()
        for skey in self.KEYS:
            if len(post) and request.POST.has_key(skey) and len(request.POST[skey]):
                if skey == self.KEYS[0]:
                        post = post.filter(question__icontains=request.POST[skey])
                elif skey == self.KEYS[1]:
                        post = post.filter(pub_date__gte=request.POST[skey])
                elif skey == self.KEYS[2]:
                        post = post.filter(pub_date__lte=request.POST[skey])
        json_serializer = serializers.get_serializer("json")()
        json_serializer.serialize(post, ensure_ascii=False, indent=4, stream=resp)
        resp['MYHEADER'] = 'abc123'
        return resp

誤ってフォーマットされた JSONish メッセージの結果:

[
    {
        "pk": 1, 
        "model": "polls.poll", 
        "fields": {
            "pub_date": "2010-08-23 22:15:07", 
            "question": "What's up?"
        }
    }
]

ピストンの JSONEmitter をバイパスして独自の JSON シリアライゼーションを行っているため、これは間違いなく起こっています。

私はピストンのemitters.pyに注いでいますが、ほとんどそれをたどることができません(私はOOP / Python / django /ピストンでかなり新しいです)。私が提供するヘッダーで補足された HTTP ヘッダーを使用して、適切にフォーマットされた JSON メッセージをピストンに配信させるにはどうすればよいですか?

4

1 に答える 1

3

をサブクラス化piston.resource.Resourceし、必要なヘッダーを__call__メソッドに追加できます。from pilot.resource import リソース

class MyResource(Resource):
    def __call__(self, request, *args, **kwargs):
        resp = super(MyResource, self).__call__(request, *args, **kwargs)
        resp['HEADER'] = "abc123"
        return resp

次に、urls.py で:

def BasicResource(handler):
    return resource.MyResource(handler=handler, authentication=basic)

your_handler = BasicResource(YourHandlerClass)
another_handler = BasicResource(AnotherHandlerClass)
于 2011-01-28T01:46:25.383 に答える