首页 > 解决方案 > python eve中的“动态”数据源?

问题描述

我怎样才能从传入的后有效负载中获得一种动态数据源?因此,所有传入的有效负载在 mongodb 中“创建”一个新集合,或者使用旧集合(如果可用)...

前任。有效载荷:{ “a1”:“999”,“d1”:“06.05.2020 04:29:16”,“d2”:“0”,“d3”:“0”,“id2”:“3777”}

settings.py 的一部分:

mdata = {
    'schema': {
            **'a1': {
                'type': 'string'**
            },
            'd1': {
                'type': 'string'
            },
            'd2': {
                'type': 'string'
            },
            'd3': {
                'type': 'string'
            },
            'id2': {
                'type': 'string'
            }
        },
    'datasource': {
            **'source': "a1-value" #should be 'a1' value from the post payload**
        },
    "resource_methods": ["POST"],
    }

DOMAIN = {
    "accounts": accounts,
    "user": user,
    "mdata": madata,
    }

标签: eve

解决方案


在 中eve,集合名称是您的资源名称:

例如,假设这是您的DOMAIN

DOMAIN = {
    'test': <your_test_endpoint_variable>,
}

test因此,当您请求此端点时,将创建一个具有名称的集合。

但是source

资源使用的数据库集合的名称。如果省略,则假定资源名称也是有效的集合名称。

我建议阅读文档的这一部分:

https://docs.python-eve.org/en/stable/config.html#advanced-datasource-patterns


推荐阅读