如何在不替换旧值的情况下添加到 Python Eve 中的列表类型 [英] How to add to a list type in Python Eve without replacing old values

查看:58
本文介绍了如何在不替换旧值的情况下添加到 Python Eve 中的列表类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与此问题中的人有非常相似的设置:如何在 Python Eve 中更新列表 data_relation具有用户资源和列表类型的朋友子资源.

I have a very similar setup to the person in this question: How do I update a list data_relation in Python Eve with a users resource and a friends sub-resource of list type.

users = {
    …
    ‘friends’: {
    'type': 'list’, 
        'schema': {
        'type': 'objectid’, 
        'data_relation': { 
            'resource': 'users’ 
        } 
    }
 }
},

但是,当我尝试向好友列表添加新值时,列表中的其他值会被新值替换.如何向列表中添加单个值并保留旧值?

However, when I try to add a new value to the friends list, the other values in the list get replaced by the new value. How do I add a single value to the list and keep the old values?

GET /users/5522987f893e3902048c55ff

{
"_updated": "Wed, 15 Apr 2015 17:22:07 GMT",
"_created": "Mon, 06 Apr 2015 14:30:23 GMT",
"_id": "5522987f893e3902048c55ff",
"friends": [
    "552e9eb0893e391063045edc"
]
}

PATCH /users/5522987f893e3902048c55ff
{"friends": ["550f288d893e390204b0a5ac"]}

RESPONSE:
{
"_updated": "Wed, 15 Apr 2015 19:38:06 GMT",
"_created": "Mon, 06 Apr 2015 14:30:23 GMT",
"_status": "OK",
"_id": "5522987f893e3902048c55ff"
}

GET /users/5522987f893e3902048c55ff

{
"_updated": "Wed, 15 Apr 2015 19:38:06 GMT",
"_created": "Mon, 06 Apr 2015 14:30:23 GMT",
"_id": "5522987f893e3902048c55ff",
"friends": [
    "550f288d893e390204b0a5ac"
]
}

我也尝试过 PUT,但它也用新值替换了列表.

I have also tried PUT, but it replaces the list with the new value as well.

我只是尝试使用 POST.

I just tried using POST.

POST /users/5522987f893e3902048c55ff/friends
{"552e9eb0893e391063045edc"}

RESPONSE:
{
"_status": "ERR",
"_error": {
    "message": "The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.",
    "code": 404
}
}

POST /users/5522987f893e3902048c55ff
{"friends": ["552e9eb0893e391063045edc"]}

RESPONSE:
{
"_status": "ERR",
"_error": {
    "message": "The method is not allowed for the requested URL.",
    "code": 405
}
}

推荐答案

POST 将允许您向 users 端点添加新文档.如果它返回 405,那么您很可能需要通过将其添加到 RESOURCE_METHODS 列表来启用 POST 方法,因为默认情况下所有端点都是只读的;请参阅 docs(或者,您只能通过将方法添加到本地 resource_methods 代替.)

POST will allow you to add a new document to the users endpoint. If it is returning a 405 then you most likely need to enable the POST method by adding it to the RESOURCE_METHODS list, as all endpoints are read-only by default; see docs (alternatively you can only enable POST for the individual endpoint by adding the method to the local resource_methods instead.)

PATCH 允许替换单个字段(而不是 PUT,它将替换整个文档).因此,使用 PATCH,您可以通过用新值(在您的情况下为新列表 ObjectIds)替换来原子地更新 friends 列表,但您不能在其中推送单个新 ObjectId现有的名单,恐怕.

PATCH allows for the replacing of individual fields (as opposed to PUT, which will replace the whole document). So with PATCH you can atomically update the friends list by replacing it with a new value (a new list ObjectIds in your case), but you cannot push a single new ObjectId inside the existing list, I am afraid.

这篇关于如何在不替换旧值的情况下添加到 Python Eve 中的列表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆