Piston customize response representation

django, django-piston, python, rest

Solution

You can return custom format by returning a Python dictionary. Here is an example on one of my app. I hope it helps.

from models import *
from piston.handler import BaseHandler
from django.http import Http404

class ZipCodeHandler(BaseHandler):
    methods_allowed = ('GET',)

    def read(self, request, zip_code):
        try:
            points = DeliveryPoint.objects.filter(zip_code=zip_code).order_by("name")
            dps = []
            for p in points:
                name = p.name if (len(p.name)<=16) else p.name[:16]+"..."
                dps.append({'name': name, 'zone': p.zone, 'price': p.price})
            return {'length':len(dps), 'dps':dps}    
        except Exception, e:
            return {'length':0, "error":e}

Problem

I am using piston and I would like to spit out a custom format for my response. My model is something like this: ``` class Car(db.Model): name = models.CharField(max_length=256) color = models.CharField(max_length=256) ``` Now when I issue a GET request to something like /api/cars/1/ I want to get a response like this: ``` {'name' : 'BMW', 'color' : 'Blue', 'link' : {'self' : '/api/cars/1'} } ``` However piston only outputs this: ``` {'name' : 'BMW', 'color' : 'Blue'} ``` In other words I want to customize the representation of a particular resource. My piston Resource handler currently looks like this: ``` class CarHandler(AnonymousBaseHandler): allowed_methods = ('GET',) model = Car fields = ('name', 'color',) def read(self, request, car_id): return Car.get(pk=car_id) ``` So I don't really get where I have the chance to customize the data. Unless I have to overwrite the JSON emitter, but that seems like a stretch.

Original source