2013-07-06 56 views
1

是否可以在tastypie中創建默認對象?我想在第一次通過REST API檢索對象時創建一個對象,所以總會有一個返回值。我可以在dehydrate這樣做,但我也需要考慮GET參數來創建對象。什麼是重載的最佳方法,以及我如何關聯對象(GET參數引用的)?Tastypie get_or_create對象

回答

0

我可能已經找到'a'解決方案。

ModelResource,我超載obj_get_list

def obj_get_list(self, bundle, **kwargs): 
    if bundle.request.method == 'GET': 
     related_id = bundle.request.GET['entity'] 
     # create new object if it doesn't exist and populate with `related_id` 
     # ... 
    objects = ModelResource.obj_get_list(self, bundle, **kwargs) 
    return objects 

URL來調用,這將有一個GET參數/url/to/resource?entity=1

這個解決方案有什麼問題嗎?任何人都可以預見不良副作用嗎?