Search code examples
djangopython-2.7tastypie

Django tastypie , how to access the object created by post_list in ModelResource class


here is my tastypie code snippet.

class MycustomeResource(ModelResource):
    asdf = fields.IntegerField('asdf', null=True)
    adsfasd = fields.IntegerField('adsfasd')

    class Meta:
        always_return_data = True
        queryset = Mycustome.objects.all()


    def post_list(self, request, **kwargs):

        object_temp = super(MycustomeResource, self).post_list(request, **kwargs)

        return object_temp

here post_list is creating on object, I want that object , so that I can perform operations on it.

do I need to override obj_create to get object..?


Solution

  • Yes, post_list method calls obj_create method of ModelResource class, and you can access your object inside it like this:

    def obj_create(self, bundle, request=None, **kwargs):
        bundle = super(MycustomeResource, self).obj_create(bundle, request)
    
        '''
        operations with your object bundle.obj
        '''
        return bundle