Tuesday, December 10, 2013

Tastypie filtering

Tastypie is a django restful api

Here is a little recipe to show only the last resource of eache model exposed as WS:



class RequestResource (ModelResource):
    """ Request webservice
    """
    class Meta:
        queryset1 = Request.objects.order_by('-id')
        queryset = queryset1.all()[:1]
        resource_name = 'requestresource'
        authorization = Authorization()

Another recipe to show also a foreing key in a given resource:

class EmployeeResource (ModelResource):
    """ Employee webservice
    """
    user = fields.ForeignKey(UserResource,'user',full=True,null=False,blank=False)

    class Meta:
        queryset = Employee.objects.all()
        resource_name = 'employeeresource'
        authorization = Authorization()

No comments: