如何将布尔值发送到python-eve?

时间:2014-03-24 00:41:16

标签: rest eve

我尝试使用python eve更新布尔值,但我总是收到相同的错误,

_issues: {deleted:must be of boolean type}
deleted: "must be of boolean type"
_status: "ERR"

我已尝试将该字段设为true(设置javascript类型)' True'并且' true' (作为文本)和1,但错误总是相同的。

startdate=2014-03-25T03%3A00%3A00.000Z&code=dsa&enddate=2014-03-31T03%3A00%3A00.000Z&name=sad&note=&deleted=True

startdate=2014-03-25T03%3A00%3A00.000Z&code=dsa&enddate=2014-03-31T03%3A00%3A00.000Z&name=sad&note=&deleted=true

startdate=2014-03-25T03%3A00%3A00.000Z&code=dsa&enddate=2014-03-31T03%3A00%3A00.000Z&name=sad&note=&deleted=1

有什么想法吗?

此致

加斯

settings.py

entity= {
    'resource_methods': ['GET', 'POST'],
    'schema': schema,
    'datasource': {
            'filter': {'$or':[{'deleted':{'$exists':0}},{'deleted':False}]}
            }
}

schema = {
'name': {
    'type': 'string',
    'minlength': 1,
    'maxlength': 50,
    'required': True,
},
'code': {
    'type': 'string',
},
'deleted':{
    'type':'boolean',
    'default':False
}
}

完整请求

Request URL:http://localhost:5000/campaign/532f797da54d75faabdb25d5
Request Method:PUT
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,no;q=0.6,es;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Content-Length:112
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:5000
If-Match:3c7bc93e3c7d60da62f350ac990c16e29b08660f
Origin:http://localhost:5000
Pragma:no-cache
Referer:http://localhost:5000/static/index.html
X-Requested-With:XMLHttpRequest
Form Dataview parsed
startdate=2014-03-25T03%3A00%3A00.000Z&code=dsa&enddate=2014-03-31T03%3A00%3A00.000Z&name=sad&note=&deleted=True
Response Headersview source
Access-Control-Allow-Headers:
Access-Control-Allow-Max-Age:21600
Access-Control-Allow-Methods:HEAD, GET, PUT, POST, DELETE, OPTIONS, PATCH
Access-Control-Allow-Origin:*
Content-Length:69
Content-Type:application/json
Date:Mon, 24 Mar 2014 00:30:10 GMT
Server:Werkzeug/0.9.4 Python/2.7.5
Set-Cookie:session=eyJfcGVybWFuZW50Ijp0cn

3 个答案:

答案 0 :(得分:1)

如果您更改架构以强制deleted值为bool,则可以发送intstr值,并在插入/更新时将其转换为bool

首先,创建一个函数将所有内容转换为bool:

to_bool = lambda v: v if type(v) is bool else str(v).lower() in ['true', '1']

然后,更改架构中的deleted以使用该函数来强制值,如下所示:

'deleted':{
    'type':'boolean',
    'coerce': to_bool,
    'default':False
}

使用此功能,您可以按照'0'0'false''False' yelding to boolean false或'1'这样的值发送已删除的示例,1'true''True'生效

答案 1 :(得分:0)

仅当您使用form-data或application/x-www-form-urlencoded的内容类型发送请求时才会发生这种情况。在AJAX请求的情况下,发送布尔值有效。

var payload = {
    "deleted": false
};

var xhr = $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(payload),
    dataType: "json",
    url: "some url"
}) 

答案 2 :(得分:-1)

你的有效载荷应该是一个像:

payload = {"somebool": False}

然后将其转换为json字符串:

payload_json = json.dumps(payload)

导致字符串中的bool值较低:

'{"f5only": false}'

然后你可以发布或修补它:

requests.post("{0}/endpoint/".format(api_server), headers=headers, data=payload_json)