Praful Bagai
Praful Bagai

Reputation: 17372

Django- Get values from Tastypie-Bundle

I'm making a RESTful API using Django-Tastypie.

I need to get(retrieve) the values POSTed/send through my form. Here is my code.

class InstallationResource(ModelResource):
    class Meta:
        queryset = Installation.objects.all()
        resource_name = 'installation'



class ApiActionsResource(ModelResource):
    installation_id = fields.ForeignKey(InstallationResource, 'installation111')
    class Meta:
        queryset = Controller.objects.all()
        resource_name = 'actions'
        allowed_methods = ['post']
        fields = ['installation_id']


    def obj_create(self, bundle, **kwargs):
        print bundle #<Bundle for obj: 'Controller object' and with data: '{'installation_id': u'related'}'>

        print kwargs #{}
        return super(EnvironmentResource, self).obj_create(bundle, user=bundle.request.user)

When I print bundle, I get <Bundle for obj: 'Controller object' and with data: '{'installation_id': u'12'}'>. I want to get the installation_id from this bundle. How do I get it? `

Upvotes: 1

Views: 1774

Answers (1)

Adriana
Adriana

Reputation: 133

The data lies within bundle.data, which is a plain Python dictionary.

You can retrieve the values like this: bundle.data.get('installation_id').

More info on bundle structures here: http://django-tastypie.readthedocs.org/en/latest/bundles.html.

Upvotes: 4

Related Questions