0

新しいプロジェクトで Tastypie を使用して RESTful API を作成しています。メソッド (Get、POST、PUT、および DELETE) ごとに個別の関数を作成し、それらの異なるロジックを処理したいと考えています。どうやってやるの?

4

1 に答える 1

1

Resourceのメソッドをオーバーライドし、dispatchそこでそれぞれの関数を作成する必要があります。単純なロジックを実行したい場合は、リソース original への呼び出しの後にコードを配置しますdispatch。コードは次のようになります。

def dispatch(self, request_type, request, **kwargs):
    response = super(Resource, self).dispatch(request_type, request, **kwargs)

    # Pass any parameters that you require to the functions
    if request.method == 'GET':
        custom_get()
    if request.method == 'POST':
        custom_post()
    if request.method == 'PUT':
        custom_put()
    if request.method == 'DELETE':
        custom_delete()

    return response

一般的には、応答でより複雑なことをしたい場合を除いて、目的には十分なはずです。

于 2013-08-30T11:01:21.013 に答える