【问题标题】:Django Tastypie: "error_message": "'bool' object has no attribute 'read'"Django Tastypie:“error_message”:“'bool'对象没有属性'read'”
【发布时间】:2013-07-16 09:04:31
【问题描述】:

我正在使用 Backbone.js + Tastypie + Django 并尝试使用 patch = true 来保存模型以更新模型上的点,例如

this.save({
  points: newPoints
}, {
  patch: true
});

这会发出一个 PUT 请求,因为它应该使用请求负载

points: 105

但是,我收到 500 错误消息

{"error_message": "'bool' object has no attribute 'read'", "traceback": "Traceback (most recent call last):

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/tastypie/resources.py\", line 217, in wrapper
    response = callback(request, *args, **kwargs)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/tastypie/resources.py\", line 468, in dispatch_detail
    return self.dispatch('detail', request, **kwargs)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/tastypie/resources.py\", line 491, in dispatch
    response = method(request, **kwargs)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/tastypie/resources.py\", line 1656, in patch_detail
    self.update_in_place(request, bundle, deserialized)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/tastypie/resources.py\", line 1679, in update_in_place
    return self.obj_update(bundle=original_bundle, **kwargs)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/tastypie/resources.py\", line 2209, in obj_update
    bundle = self.full_hydrate(bundle)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/tastypie/resources.py\", line 909, in full_hydrate
    value = field_object.hydrate(bundle)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/tastypie/fields.py\", line 382, in hydrate
    value = make_aware(parse(value))

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/dateutil/parser.py\", line 720, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/dateutil/parser.py\", line 308, in parse
    res = self._parse(timestr, **kwargs)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/dateutil/parser.py\", line 356, in _parse
    l = _timelex.split(timestr)

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/dateutil/parser.py\", line 150, in split
    return list(cls(s))

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/dateutil/parser.py\", line 147, in next
    return self.__next__()  # Python 2.x support

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/dateutil/parser.py\", line 141, in __next__
    token = self.get_token()

  File \"/Users/me/.virtualenvs/project/lib/python2.7/site-packages/dateutil/parser.py\", line 72, in get_token
    nextchar = self.instream.read(1)

AttributeError: 'bool' object has no attribute 'read'
"}

这是我的 UserProfile 模型资源

class UserProfileResource(ModelResource):

    """A resource for the UserProfile model."""

    class Meta:
        queryset = UserProfile.objects.all()
        resource_name = 'userprofile'
        authorization = Authorization()
        excludes = ['field_to_exclude']
        always_return_data = True

有人有关于如何调试此错误的提示吗?

【问题讨论】:

  • 这是服务器端故障。你能在处理请求的服务器上显示一些代码吗?
  • 我已经添加了我的服务器代码——我正在使用tastepie,所以我唯一的自定义是定义资源。由于它使用的是库而不是我的代码,我不太确定如何跟踪它

标签: django rest backbone.js tastypie


【解决方案1】:

我有一个字段,它是一个为 null 的 datetimefield,它没有在 api 调用中显示为 datetime,而是显示为一个布尔值。我不知道为什么会发生这种情况,但我认为这是 sweetpie 中的一个错误,或者我对配置有误解,或者两者兼而有之。

我从tastepie 资源中排除了这个字段,我能够成功地对模型进行PUT 和PATCH。

【讨论】:

    猜你喜欢
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2016-04-08
    • 2015-05-17
    • 1970-01-01
    • 2013-10-28
    相关资源
    最近更新 更多