Django- Get values from Tastypie-Bundle

橙三吉。 提交于 2019-12-13 21:34:47

问题


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? `


回答1:


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.



来源:https://stackoverflow.com/questions/21530021/django-get-values-from-tastypie-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!