Search code examples
pythondjangotastypie

POST urls in tastypie


i'm new to tastypie in django. I have an organisation model. In api.py

class OrganisationResource(ModelResource):
    create_user = fields.ForeignKey(PersonResource, 'create_user', null=True, full=True)
    update_user = fields.ForeignKey(PersonResource, 'update_user', null=True, full=True)
    location = fields.ForeignKey(LocationResource, 'location', null=True, full=True)
    class Meta:
        allowed_methods = ['post','get','delete','patch','put']
        queryset = Organization.objects.all()
        resource_name = 'organisation'
        authorization = Authorization()
        authentication = Authentication()
        always_return_data = True

The api url is,

http://127.0.0.1:8000/api/v1/organisation/

A post request to the above link is saving that data to database. But my doubt is can i use a seperate link to post or overwrite the current url so that i can send the post request to that link . Like

http://127.0.0.1:8000/api/v1/organisation/create

Solution

  • You can create seperate url for the Post using prependurls useful link

    class OrganisationCreateResource(ModelResource):
        create_user = fields.ForeignKey(PersonResource, 'create_user', null=True, full=True)
        update_user = fields.ForeignKey(PersonResource, 'update_user', null=True, full=True)
        location = fields.ForeignKey(LocationResource, 'location', null=True, full=True)
        class Meta:
            allowed_methods = ['post']
            queryset = Organization.objects.all()
            detail_uri_name = 'create'
            resource_name = 'organisation'
            authorization = Authorization()
            authentication = Authentication()
            always_return_data = True
       def prepend_urls(self):
            return [
                url(r'^(?P<resource_name>%s)/create/' % self._meta.resource_name, self.wrap_view('dispatch_detail'), name='api_dispatch_detail'),
            ]