不能在django / tastypie中按关系过滤

时间:2013-06-07 21:29:37

标签: python django tastypie

假设:

class CheckResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user', full=True)
    venue = fields.ForeignKey(VenueResource, 'venue', full=False)
    items = fields.ToManyField('checks.api.CheckToItemResource',
                               'checktoitem_set',
                               related_name='checktoitem',
                               full=True,
                               null=True,
                               blank=True
                               )
    payments = fields.ToManyField('payments.api.PaymentResource',
                                  'payment_set',
                                  related_name='check', 
                                  full=True,
                                  null=True,
                                  blank=True
                                  )
    authorizations = fields.ToManyField('authorizenet.api.AuthorizenetResource',
                                        'response_set',
                                        related_name='check',
                                        full=True,
                                        null=True,
                                        blank=True
                                        )

    class Meta:
        queryset = Check.objects.filter(checktoitem__is_void=False)
        allowed_methods = ['get', 'post', 'put']
        include_resource_uri = False
        always_return_data = True
        ordering = ['number', 'is_closed']
        filtering = {
            'slug': 'exact',
            'checktoitem': ALL
        }


class CheckToItemResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user', full=True)
    venue = fields.ForeignKey(VenueResource, 'venue', full=False)
    check = fields.ForeignKey(CheckResource, 'check', full=False)
    item = fields.ForeignKey(ItemResource, 'item', full=True)
    parent_item = fields.ForeignKey(ItemResource, 'parent_item', full=False, null=True)

    class Meta:
        queryset = CheckToItem.objects.filter(is_void=False)
        allowed_methods = ['get', 'post', 'put']
        include_resource_uri = False
        always_return_data = True
        filtering = {
            'check': 'exact',
            'is_void': ALL
        }
        ordering = ['created']

请注意,我正在尝试仅检索其中is_void = False的checktoitem记录。但是,返回所有记录(不仅仅是无记录的记录)。有帮助吗?我尝试在几个地方添加过滤器,但没有任何工作。

1 个答案:

答案 0 :(得分:0)

你能表明你提出的要求吗? 请求中可能存在错误而不是代码。

使用python请求模块的示例请求:

import requests
r = request.get('http://localhost:8000/api/checktoitem/?is_void=False', auth=('user', 'pass'), headers={'content-type': 'application/json'})