开发者

piston-django how many methods should a single handler contain

开发者 https://www.devze.com 2023-03-14 22:51 出处:网络
I\'ve been building a handler class for each 开发者_运维技巧method I want to map to the url file. Is my approach correct or wrong? because I don\'t seem to find a way to map a resource to a method the

I've been building a handler class for each 开发者_运维技巧method I want to map to the url file. Is my approach correct or wrong? because I don't seem to find a way to map a resource to a method they all map to an entire class.

Regards,


The documentations seems very clear https://bitbucket.org/jespern/django-piston/wiki/Documentation#!resources

from piston.handler import BaseHandler
from myapp.models import Blogpost

class BlogpostHandler(BaseHandler):
   allowed_methods = ('GET',)
   model = Blogpost   

   def read(self, request, post_slug):
      ...

Piston lets you map resource to models, and by doing so, it will do a lot of the heavy lifting for you.

A resource can be just a class, but usually you would want to define at least 1 of 4 methods:

read is called on GET requests, and should never modify data (idempotent.)

create is called on POST, and creates new objects, and should return them (or rc.CREATED.)

update is called on PUT, and should update an existing product and return them (or rc.ALL_OK.)

delete is called on DELETE, and should delete an existing object. Should not return anything, just rc.DELETED.

Also https://bitbucket.org/jespern/django-piston/wiki/Documentation#!mapping-urls

In urls.py:

from django.conf.urls.defaults import *
from piston.resource import Resource
from mysite.myapp.api.handlers import BlogpostHandler

blogpost_handler = Resource(BlogpostHandler)

urlpatterns = patterns('',
   url(r'^blogpost/(?P<post_slug>[^/]+)/', blogpost_handler),
   url(r'^blogposts/', blogpost_handler),
)
0

精彩评论

暂无评论...
验证码 换一张
取 消