Interview Questions

Python - TastyPie Create a Model Resource

The following is a sample code to create a ModelResource in TastyPie.

import copy
from tastypie.resources import ModelResource
from tastypie.authentication import Authentication
from tastypie.authorization import Authorization
from django.db import models

class DeviceResource(ModelResource):    
    class Meta:        
        queryset = Device.objects.all()      
        resource_name = 'device'
        excludes = ['pk']
        include_resource_uri = False
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization()
    def get_object_list(self, request):                        
            print ("object Get List ")                  
            # write your code to process pre HTTP GET method handling                    
            return super(DeviceResource, self).get_object_list(request);
    def alter_list_data_to_serialize(self, request, data_dict):        
        if isinstance(data_dict, dict):
            if 'meta' in data_dict:
                data_dict['devices'] = copy.copy(data_dict['objects'])
        return data_dict

class Device(models.Model):
    id = models.CharField(max_length=70,primary_key=True)
    name = models.CharField(max_length=70)
    def __unicode__(self):