Django Tastypie多功能过滤器

Django Tastypie多功能过滤器,django,tastypie,Django,Tastypie,我无法将筛选器应用于与tastypie的多个关系。这是我的模型 class Post(models.Model): user = models.ForeignKey(User, related_name='posts') title = models.TextField() class Meta: """ Meta """ db_table = "post" class Category(models.Model): posts

我无法将筛选器应用于与tastypie的多个关系。这是我的模型

class Post(models.Model):
    user = models.ForeignKey(User, related_name='posts')
    title = models.TextField()

    class Meta:
        """ Meta """
        db_table = "post"

class Category(models.Model):
    posts = models.ManyToManyField(Post, through='PostCategory', null = True, blank = True)
    name = models.TextField()

    class Meta:
        """ Meta """
        db_table = "category"

class PostCategory(models.Model):
    post = models.ForeignKey(Post, related_name='posts')
    category = models.ForeignKey(Category, related_name='categories')

    class Meta:
        """ Meta """
        db_table = "post_category"
这里是api.py

class CategoryResource(ModelResource):
    class Meta:
        queryset = Category.objects.all()
        resource_name = 'category'
        filtering = {
            'id': ['exact'],
        }

class PostCategoryResource(ModelResource):
    category = fields.ToOneField(CategoryResource, 'category', full=True)
    class Meta:
        queryset = PostCategory.objects.all()
        resource_name = 'postcategory'
        filtering = {
            'category': ALL_WITH_RELATIONS,
            'id': ['exact'],
        }

class PostResource(ModelResource):
    user = fields.ToOneField(ProfileResource, 'user', full=True)
    categories = fields.ToManyField(PostCategoryResource,
            attribute=lambda bundle: bundle.obj.categories.through.objects.filter(
                post=bundle.obj,)
            or bundle.obj.categories, full=True)

    class Meta:
        queryset = Post.objects.all()
        resource_name = 'post'
        filtering = {
            'user': ALL_WITH_RELATIONS,
            'categories': ALL_WITH_RELATIONS,
            'id': ['exact'],
        }

我的目标是能够按用户id或类别id获取所有帖子。以下内容正是我想要的:
http://127.0.0.1/api/v1/post/?format=json&user__id=2


然而,我无法让类似这样的东西也正常工作:
http://127.0.0.1/api/v1/post/?format=json&categories__category__id=3


下面是我得到的错误:

{“error_message”:“sequence item 0:expected string,function found”,“traceback”:“traceback>(最近一次调用):\n\n File\”/usr/lib/python2.6/site packages/django_tastype-0.11.0->py2.6.egg/tastype/resources.py\”,第195行,在包装器response=callback(请求,>*args,**kwargs)\n\n File\”/usr/lib/python2.6/site packages/django_-tastypie-0.11.0->py2.6.egg/tastypie/resources.py\”,第426行,在dispatch\u list\n return>self.dispatch('list',request,**kwargs)\n\n File\”/usr/lib/python2.6/site->packages/django_-tastypie-0.11.0-py2.6.0.egg/tastypie/resources.py\,第458行,在dispatch\n>response=method(request,**,**,**\n\n文件\“/usr/lib/python2.6/site->packages/django\u-tastypie-0.11.0-py2.6.egg/tastypie/resources.py\”,第1266行,在get\u list\n>objects=self.obj\u-get\u-list(bundle=base\u bundle,**self.remove\u-api\u-resource\u\u-names(kwargs))\n\n\n>文件\“/usr/lib/python2.6/site-packages/django\u-tastypie-0.11.0.6/pyypie/typi2.6.6/py.py\“,第2044行,在obj\u get\u list\n applicative\u filters=>self.build\u filters(filters=filters)\n\n File\”/usr/lib/python2.6/site->packages/django\u tastypi-0.11.0-py2.6.egg/tastypi/resources.py\”,第1949行,在>build\n db\u filters\n db\u字段\u name=LOOKUP\u SEP.join(LOOKUP\n查找\n位)\n\n peerror:sequence item>0:预期字符串,函数已找到”}


当字段属性由lambda表示时,tasty似乎不允许按虚拟字段进行过滤

您可以通过一些修改使其工作:

class PostCategory(models.Model):
    post = models.ForeignKey(Post, related_name='categories')
    category = models.ForeignKey(Category)


class PostResource(ModelResource):
    user = fields.ToOneField(ProfileResource, 'user', full=True)
    categories = fields.ToManyField(PostCategoryResource, 'categories', full=True)

    class Meta:
        queryset = Post.objects.all()
        resource_name = 'post'
        filtering = {
            # filter by user
            'user': ALL_WITH_RELATIONS,

            #filter by category id
            'categories': ALL_WITH_RELATIONS,
            'category': ALL,
            'id': ['exact'],
        }

今天,当我在尝试筛选属性为可调用字段的字段时收到一个tastype错误时,我偶然发现了这个线程:
TypeError:sequence item 1:expected string或Unicode,function find

tastypie似乎要求属性必须是字符串才能进行筛选,但如果要筛选嵌套对象,则必须是可调用的。为了解决这一问题,我实现了一个
CallableString
,这是一个可以调用的字符串:

class CallableString(unicode):
    def __init__(self, *args, **kwargs):
        super(CallableString, self).__init__(*args, **kwargs)

    def __call__(self, bundle):
        return InstitutionGroup.objects.published().filter(institutions=bundle.obj)
用您自己的自定义逻辑替换
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

groups = fields.ToManyField(
    InstitutionGroupResource,
    attribute=CallableString('institution_groups'),
    null=True,
    full=True,
)
我会说这是一个非常黑客的解决方案,可能会有一些副作用,但现在似乎对我来说是可行的-tastypie版本0.13.3

groups = fields.ToManyField(
    InstitutionGroupResource,
    attribute=CallableString('institution_groups'),
    null=True,
    full=True,
)