问题
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