根据手册应该可以
给定一个对象一个数据字典或两者,构建一个Bundle,以便在整个脱水/水合循环中使用。
所以我使用Resource
将它们放在一起def get_list(self, request, **kwargs):
bundles = []
foo = {}
foo['bar'] = 1
bundle = self.build_bundle(data=foo, request=request)
bundles.append(self.full_dehydrate(bundle))
serialized = {}
serialized[self._meta.collection_name] = bundles
serialized = self.alter_list_data_to_serialize(request, serialized)
return self.create_response(request, serialized)
但在full_hydrate()
bar
中,即使我通过它,它也会在Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
77. return view_func(*args, **kwargs)
File "/home/dan/project/tastypie/resources.py" in wrapper
203. response = callback(request, *args, **kwargs)
File "/home/dan/project/tastypie/resources.py" in dispatch_list
445. return self.dispatch('list', request, **kwargs)
File "/home/dan/project/tastypie/resources.py" in dispatch
477. response = method(request, **kwargs)
File "/home/dan/project/resources/widgets.py" in get_list
188. base_bundle = self.build_bundle(request=request)
File "/home/dan/project/tastypie/resources.py" in build_bundle
704. obj = self._meta.object_class()
Exception Type: TypeError at /api/widget/fundperfyearly/
Exception Value: 'NoneType' object is not callable
上窒息。我在这里缺少什么?
{{1}}
答案 0 :(得分:5)
您尚未在模型资源的queryset
类中指定object_class
或Meta
。使用ModelResource
时,至少应指定其中一个。
您可以通过将obj
作为参数传递给build_bundle
方法来避免这种情况,例如
bundle = self.build_bundle(obj=MyModel(), data=foo, request=request)
Tastypie v0.9.15