简体   繁体   中英

Django Tastypie create API which accepts POST data but not creates any entry in database

My question is exactly what it's subject says :
How to create Django Tastypie API which accepts the POST data, does some processing on it and returns some HTTP response, but does not creates any entry in database.


For Example for this sample API resource :

class NextNumberResource(ModelResource):
class Meta:
    resource_name = 'next_number'
    detail_allowed_methods = []
    list_allowed_methods = ['post']


def obj_create(self, bundle, **kwargs):

    #raise CustomBadRequest(code = "code ={c}".format(c=int(bundle.data["number"])*2))
    next_number = int(bundle.data["number"]) * 2
    data = json.dumps({"next_number":next_number})
    return HttpResponse(data, content_type='application/json', status=200)

I am getting following error :
{"error_message": "'HttpResponse' object has no attribute 'pk'"}

I think it's better to handle this request in dispatch_* methods (eg dispatch_list).

For example here .

Explanation: If you handle a post request which doesn't create any instance, you have to process it before std workflow of the tastypie.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM