1
是否可以在tastypie中創建默認對象?我想在第一次通過REST API檢索對象時創建一個對象,所以總會有一個返回值。我可以在dehydrate
這樣做,但我也需要考慮GET
參數來創建對象。什麼是重載的最佳方法,以及我如何關聯對象(GET參數引用的)?Tastypie get_or_create對象
是否可以在tastypie中創建默認對象?我想在第一次通過REST API檢索對象時創建一個對象,所以總會有一個返回值。我可以在dehydrate
這樣做,但我也需要考慮GET
參數來創建對象。什麼是重載的最佳方法,以及我如何關聯對象(GET參數引用的)?Tastypie get_or_create對象
我可能已經找到'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
。
這個解決方案有什麼問題嗎?任何人都可以預見不良副作用嗎?