2👍
I would probably skip the finer-grained things like hydrate, apply_sorting, build_filters, etc.
I’m assuming that without objects behind the api you’re using a list-looking url like /api/v1/add_stuff/
, and assuming you’re accepting POST requests. If these assumptions are wrong you can adjust by changing to post_detail, get_list, etc.
def post_list(self, request, **kwargs):
_a = request.POST.get('first_number', None)
_b = request.POST.get('second_number', None)
if None in (_a, _b):
raise HttpBadRequest()
return self.create_response(request, {'result': _a + _b})
Note that I think this code would work but I haven’t tested it. It’s meant to provide a starting point.
This section of the Tastypie docs describes the order in which the various methods are called, and toward the bottom of the page there is a full API reference so you can see what parameters things expect and what they are supposed to return.
Edit:
The flow for this situation will look something like this:
-
In
dispatch
, the request uri is inspected. Depending on whether a
detail or a list uri was requested (/api/v1/add_stuff/<pk>/
or
/api/v1/add_stuff/
), handling is delegated todispatch_detail
or
dispatch_list
. This is also where authentication, authorization,
and throttling checks happen. -
In
dispatch_list
, the request method is inspected and the call is
delegated to a method named'%s_list' % request.METHOD.lower()
.
To answer your comment, these are magical method names. If the
request method is POST,dispatch_list
looks for a method named
post_list
and an error is thrown if the appropriate handler
is not defined.