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

后端 未结 2 1023
盖世英雄少女心
盖世英雄少女心 2021-01-04 21:24

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.

2条回答
  •  悲&欢浪女
    2021-01-04 21:50

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

    Instead of having a users/friends list like I had 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 I query crystals//shares, I should get a list of the shares for that crystal.

    I think you could apply a solution like this to the users/friends scenario if you swap crystals for users and shares for friends. You might 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//shares',
        'datasource': {'source': 'shares'},
    }
    

提交回复
热议问题