2013-06-05 54 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