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

这个解决方案有什么问题吗?任何人都可以预见不良副作用吗?