tastypie - 在另一个m2m内移除m2m

时间:2013-06-25 21:09:53

标签: django tastypie

我有两个互相拥有外键的资源,例如:

class GroupResource(ModelResource):
    categories = fields.ToManyField(CategoryResource, 'category_set', 'categories', full=True)

    class Meta:
        queryset = Group.objects.all()


class CategoryResource(ModelResource):
    subcategories = fields.ToManyField(SubcategoryResource, 'subcategory_set', 'subcategories', full=True)

    class Meta:
        queryset = Category.objects.all()

当我列出GroupResource时,它还会列出subcategories。如何阻止,只列出categories而不是subcategories

2 个答案:

答案 0 :(得分:1)

根据您在列出时所包含的详细程度,您可以尝试在类别关系上设置full_list=False,或使用子类别上的full_listfull_detail来更改输出。

http://django-tastypie.readthedocs.org/en/latest/fields.html#tastypie.fields.RelatedField.full_list

答案 1 :(得分:0)

只需将use_in='list'添加到Category

中的子类别即可
class CategoryResource(ModelResource):
  subcategories = fields.ToManyField(SubcategoryResource, 'subcategory_set', 'subcategories', full=True, use_in='list')

  class Meta:
    queryset = Category.objects.all()