[Fixed]-How to run a function with Tastypie API request?

1👍

You can run this function in the api.py or in the models.py, depending on what exactly you want to do.

The example below shows an example to run a function if you do a post at the url /lol/:

from tastypie.resources import ModelResource
from tastypie.utils import trailing_slash


class LolResource(ModelResource):

    class Meta:
        resource_name = 'lol'
        allowed_methods = ['post']  # note that this resource just accept posts

    def base_urls(self):
        return [
            url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('lol_api_function'), name="api_lol_api_function"),
        ]

    def lol_api_function(self, request, **kwargs):
        # you function
        pass

Do not forget to add the LolResource at your urls.py.

Leave a comment