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/