1

TastyPieを使用して、システム レベルのビルド ポスト テストに関するデータにアクセスするための REST クライアント/サーバーを作成しています。製品が外部キーとして内部に保存されているという事実をクライアントが無視し、製品名とタグ名を使用して CRUD 操作を実行できるようにしたいと考えています。本質的には、クライアント スクリプトが "製品" および "タグ" を CharFields として操作し、この情報をサーバーに外部キーとして保存する必要があります。ここに私のapi.pyがあります:

from tastypie import fields
from tastypie.resources import ModelResource
from models import Test, Product, Tag

class ProductResource(ModelResource):
    name = fields.CharField('name', unique=True)
    class Meta:
        queryset = Product.objects.all()
        filtering = {'iexact', 'exact'}

class TagResource(ModelResource):
    name = fields.CharField('name', unique=True)
    class Meta:
        queryset = Tag.objects.all()
        filtering = {'iexact', 'exact'}

class TestResource(ModelResource):
    product = fields.ForeignKey(ProductResource, 'product', full=True)
    tags = fields.ForeignKey(TagResource, 'tags', full=True)
    command = fields.CharField('command')
    class Meta:
        queryset = Test.objects.all()
        filtering = {'product': tastypie.constants.ALL_WITH_RELATIONS,
                     'tag': tastypie.constants.ALL_WITH_RELATIONS}

私は現在、独自のハイドレートとデハイドレートを使用してこれを行うカスタム ApiField クラスに取り組んでいますが、おそらく何かが欠けているように感じました。クライアントに次のことを許可するにはどうすればよいですか。

curl -H "Content-Type: application/json" -X POST --data '{"product": "fisherman", "command": "go fish"}' /api/v1/test/
4

1 に答える 1

0

コマンドを処理するために新しい URL を先頭に追加できます。

class TestResource(ModelResource):
   ...
   def prepend_urls(self):
      return [
        url(r"^(?P<resource_name>%s)/commands$" % self._meta.resource_name, self.wrap_view('handle_commands'), name="api_handle_commands"),
    ]


def handle_commands(self, request):
   command = request.POST['command']
   product = Product.objects.get(name=request.POST['product'])
   # do your stuff
于 2013-04-30T08:43:19.667 に答える