2013-06-05 57 views
2

我有这个作为我的资源tastypie - 如何过滤通用外键?

class AResource(ModelResource): 
    content_object = GenericForeignKeyField({ 
     B: BResource, 
    }, 'content_object') 

class Meta: 
    queryset = A.objects.all() 
    filtering = { 
     'content_type': ????, 
    } 

我想通过资源BResource的ID进行过滤,这种资源。
我试图通过这个网址进行过滤:
http://domain.com/api/v1/a/?content_type=/api/v1/b/7/
http://domain.com/api/v1/a/?content_object=/api/v1/b/7/
,但没有奏效。

我们该如何过滤?

回答

1

我知道这是一个老问题,但如果任何人发现它,我可以通过以下操作来解决这个相同的问题:

filtering = { 
    'object_id': 'exact', 
    'content_type': 'exact', 
} 

那么你的资源的URL看起来像:

在OP的情况下
http://domain.com/api/v1/a/?content_type=app_name,model_name&object_id=object_id 

所以,假设BResource是在MyApp的和型号名称是b_model

http://domain.com/api/v1/a/?content_type=myapp,b_model&object_id=7