Tastypie建筑捆绑与字典

时间:2013-05-07 21:59:16

标签: tastypie

根据手册应该可以

  

给定一个对象一个数据字典或两者,构建一个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}}

1 个答案:

答案 0 :(得分:5)

您尚未在模型资源的queryset类中指定object_classMeta。使用ModelResource时,至少应指定其中一个。

您可以通过将obj作为参数传递给build_bundle方法来避免这种情况,例如

bundle = self.build_bundle(obj=MyModel(), data=foo, request=request)

参考

Tastypie v0.9.15

  1. build_bundle
  2. Setting object_class using queryset.model if queryset specified.