如何使用Tastypie通过相关模型属性过滤API结果?

时间:2012-04-13 19:23:55

标签: django tastypie

鉴于以下API定义,我需要能够按产品别名过滤事件。

鉴于一个事件属于一个Job而一个Job属于一个产品,我不知道如何规范它。

api.py:

class ProductResource(ModelResource):

    class Meta:
        queryset = Product.objects.all()
        resource_name = 'product'
        allowed_methods = ['get']
        excludes = ['created_at','updated_at']
        filtering = {
            'alias': ALL
        }

class EnvironmentResource(ModelResource):

    class Meta:
        queryset = Environment.objects.all()
        resource_name = 'environment'
        allowed_methods = ['get']
        excludes = ['created_at','updated_at']

class JobResource(ModelResource):

    product = fields.ForeignKey(ProductResource, 'product')

    class Meta:
        queryset = Job.objects.all()
        resource_name = 'job'
        allowed_methods = ['get']
        excludes = ['created_at','updated_at']

class EventResource(ModelResource):

    environment = fields.ForeignKey(EnvironmentResource, 'environment',full=True)
    job = fields.ForeignKey(JobResource, 'job',full=True)

    class Meta:
        queryset = Event.objects.all()
        resource_name = 'event'
        allowed_methods = ['get']
        excludes = ['created_at','updated_at']
        filtering = {
            HOW DO I FILTER BY PRODUCT ALIAS????
        }

2 个答案:

答案 0 :(得分:11)

鉴于以下过滤规范:

# In EventResource
filtering = { 
   'job' : ALL_WITH_RELATIONS
}

# In JobResource
filtering = { 
   'product' : ALL_WITH_RELATIONS
}

# In ProductResource
filtering = {
    'alias' : ALL
}

你应该可以这样做:

/api/events/job__product__alias=something

答案 1 :(得分:0)

实现这一目标的一种简单方法是Advanced Filtering。这避免了由tastypie ForeignKey字段生成的许多查询。为了安全起见,请务必在将数据添加到orm_filters之前手动验证或清理过滤器输入数据。

class EventResource(ModelResource):

    class Meta:
        queryset = Event.objects.all()
        resource_name = 'event'
        allowed_methods = ['get']

    def build_filters(self, filters=None):

        if filters is None:
            filters = {}

        orm_filters = super(EventResource, self).build_filters(filters)

        # Your filtering
        if 'job__product__alias' in filters:
            orm_filters['job__product__alias'] = filters.get(
                'job__product__alias')

    return orm_filters

你应该能够得到过滤后的结果:

/api/events/?job__product__alias=something