Tastypie 在数据被请求替换之前访问查询
Tastypie Accessing Query Before Data Gets Replaced by Request
我在 tastypie 中使用 PATCH 请求来更新模型中的字段,但是当这个 PATCH 通过时,我试图附加数据库中已有的数据,而不是完全替换值。
Example:
In a blog post, a user flags a post as inappropriate. You want to get this user's UserID and append it to a field, flaggedUserID. flaggedUserID will contain this user's ID along with anyone else who has flagged the post previously.
我苦苦挣扎的地方是在 tastypie 中找到一个挂钩,通过它我可以在将值从请求复制到查询数据之前访问查询。我尝试了 "alter_deserialized_detail_data(self, request, data)" 挂钩,但传递给此函数的数据参数中的值已被替换。
有人有什么建议吗?
在阅读了tastypie的源代码后,这是我得出的解决方案:
从请求到查询的数据复制似乎发生在 "update_in_place" 函数中。通过以下方式覆盖此函数,我能够实现我正在寻找的结果:
def update_in_place(self, request, original_bundle, new_data):
"""
Update the object in original_bundle in-place using new_data.
"""
"""Right here I am checking if the parameter flaggedUserID is in the
request (PATCH saves request data to request.body). If this is false then
copy the new data into the query, otherwise jump to
alter_deserialized_detail_data where I am handling the appending of the data
from the request. """
if 'flaggedUserID' not in request.body:
original_bundle.data.update(**dict_strip_unicode_keys(new_data))
# Now we've got a bundle with the new data sitting in it and we're
# we're basically in the same spot as a PUT request. SO the rest of this
# function is cribbed from put_detail.
self.alter_deserialized_detail_data(request, original_bundle.data)
kwargs = {
self._meta.detail_uri_name: self.get_bundle_detail_data(original_bundle),
'request': request,
}
return self.obj_update(bundle=original_bundle, **kwargs)
根据 objective,我可能会建议再清理一下。我只是想以最基本的形式展示这段代码,而不必 post 多个函数并开始混淆人们。
我在 tastypie 中使用 PATCH 请求来更新模型中的字段,但是当这个 PATCH 通过时,我试图附加数据库中已有的数据,而不是完全替换值。
Example: In a blog post, a user flags a post as inappropriate. You want to get this user's UserID and append it to a field, flaggedUserID. flaggedUserID will contain this user's ID along with anyone else who has flagged the post previously.
我苦苦挣扎的地方是在 tastypie 中找到一个挂钩,通过它我可以在将值从请求复制到查询数据之前访问查询。我尝试了 "alter_deserialized_detail_data(self, request, data)" 挂钩,但传递给此函数的数据参数中的值已被替换。
有人有什么建议吗?
在阅读了tastypie的源代码后,这是我得出的解决方案:
从请求到查询的数据复制似乎发生在 "update_in_place" 函数中。通过以下方式覆盖此函数,我能够实现我正在寻找的结果:
def update_in_place(self, request, original_bundle, new_data):
"""
Update the object in original_bundle in-place using new_data.
"""
"""Right here I am checking if the parameter flaggedUserID is in the
request (PATCH saves request data to request.body). If this is false then
copy the new data into the query, otherwise jump to
alter_deserialized_detail_data where I am handling the appending of the data
from the request. """
if 'flaggedUserID' not in request.body:
original_bundle.data.update(**dict_strip_unicode_keys(new_data))
# Now we've got a bundle with the new data sitting in it and we're
# we're basically in the same spot as a PUT request. SO the rest of this
# function is cribbed from put_detail.
self.alter_deserialized_detail_data(request, original_bundle.data)
kwargs = {
self._meta.detail_uri_name: self.get_bundle_detail_data(original_bundle),
'request': request,
}
return self.obj_update(bundle=original_bundle, **kwargs)
根据 objective,我可能会建议再清理一下。我只是想以最基本的形式展示这段代码,而不必 post 多个函数并开始混淆人们。