首先做一個TagResource
:
from taggit.models import Tag
class TagResource(ModelResource):
class Meta:
queryset = Tag.objects.all()
然後在你的資源,進行了標籤:
class FooResource(ModelResource):
tags = fields.ToManyField(TagResource, 'tags', # if your tag field is 'tags'
full = True)
class Meta:
queryset = Foo.objects.all()
它應該工作。
UPDATE
爲了過濾標籤,你必須通過TagResource
對其進行過濾,假設您的API名稱是v1
,網址是:
/api/v1/tag/?slug=anytagyouwant&format=json
上述網址是像:'是anytagyouwant
存在嗎?'
爲「獲得具有anytagyouwant
標籤的所有富」
/api/v1/foo/?tags__slug=anytagyouwant&format=json
注意,要能篩選某些字段,你必須聲明它在你的資源,使用FooResource
爲例:
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
class FooResource(ModelResource):
tags = fields.ToManyField(TagResource, 'tags', # if your tag field is 'tags'
full = True)
class Meta:
queryset = Foo.objects.all()
filtering = dict(
tags = ALL,
# or
tags = ALL_WITH_RELATIONS,
)
謝謝。我怎樣才能做標籤過濾器? – Mutant
我更新了答案 –
關於第一個網址,你需要在TagResource添加過濾 - 過濾= { \t \t \t「塞」:ALL, \t \t}而這也返回一個標記對象,而不是Foo對象。我正在嘗試將所有Foo對象與'anytagyouwant'標籤映射到哪裏 – Mutant