Django-从Tastypie-Bundle获取价值 [英] Django- Get values from Tastypie-Bundle

查看:83
本文介绍了Django-从Tastypie-Bundle获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django-Tastypie制作RESTful API。

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)

当我打印纸包时,我得到< Bundle for obj:'Controller对象和数据: {'installation_id:u'12'}'> 。我想从此捆绑包中获取 installation_id 。我如何得到它?
`

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

推荐答案

数据位于bundle.data中,这是一个简单的Python字典。

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

您可以像这样检索值: bundle.data.get('installation_id')。

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

有关捆绑结构的更多信息,请参见: http://django-tastypie.readthedocs.org/en/latest/bundles.html

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

这篇关于Django-从Tastypie-Bundle获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆