How to add to list type in Python Eve without replacing old values

I have a very similar setup for the person in this question: How to update list_relation data in Python Eve with a user resource and a friends sub-resource like list.

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

However, when I try to add a new value to my friends list, other values ​​in the list are replaced with the new value. How to add one value to the list and keep 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" ] } 

I also tried PUT, but it also replaces the list with a new value.

EDIT: 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 } } 

and

 POST /users/5522987f893e3902048c55ff {"friends": ["552e9eb0893e391063045edc"]} RESPONSE: { "_status": "ERR", "_error": { "message": "The method is not allowed for the requested URL.", "code": 405 } } 
+7
python rest eve
source share
2 answers

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

PATCH allows you to replace individual fields (unlike PUT, which replaces the entire document). Thus, with PATCH, you can atomically update the friends list, replacing it with a new value (the new ObjectIds list in your case), but you cannot click one new ObjectId inside the existing list, I'm afraid.

+1
source share

Here is the workaround I used. This is probably not the most efficient way to do this, but it worked for me.

Instead of having a list of users/friends as I originally planned, I ended up creating a resource for shares. . In my case, I wanted to know which crystals were shared with which users. So when querying crystals/<crystal_id>/shares, I should get a list of shares for this crystal.

I think you could apply such a solution to the users/friends scenario if you change crystals to users and shares to friends. . You can put two different user_id data_relations in your friends_schema (my shares_schema ).

 shares_schema = { 'crystal_id': { 'type': 'objectid', 'required': True, 'data_relation': { 'resource': 'crystals', 'embeddable': True, }, }, 'user_id': { 'type': 'objectid', 'required': True, 'data_relation': { 'resource': 'users', 'embeddable': True, }, }, } shares = { 'internal_resource': True, 'schema': shares_schema, } crystals_shares = { 'schema': shares_schema, 'url': 'crystals/<regex("[a-f0-9]{24}"):crystal_id>/shares', 'datasource': {'source': 'shares'}, } 
+1
source share

All Articles