1

下面的代码是 Python Quickbase 模块的一部分,已经有一段时间没有更新了。下面显示的函数之一的帮助文本不清楚如何传递参数以上传文件(其值实际上是 base64 编码的)。

def add_record(self, fields, named=False, database=None, ignore_error=True, uploads=None):
    """Add new record. "fields" is a dict of name:value pairs
    (if named is True) or fid:value pairs (if named is False). Return the new records RID
    """
    request = {}
    if ignore_error:
        request['ignoreError'] = '1'
    attr = 'name' if named else 'fid'
    request['field'] = []
    for field, value in fields.iteritems():
        request_field = ({attr: to_xml_name(field) if named else field}, value)
        request['field'].append(request_field)
    if uploads:
        for upload in uploads:
            request_field = (
                {attr: (to_xml_name(upload['field']) if named else upload['field']),
                 'filename': upload['filename']}, upload['value'])
            request['field'].append(request_field)

    response = self.request('AddRecord', database or self.database, request, required=['rid'])
    return int(response['rid'])

有人可以帮助我如何传递参数以添加记录。

4

1 回答 1

1

根据您提供的定义,您似乎需要传递一个字典数组,每个字典都为uploads参数提供字段名称/id、文件名和文件的 base64 编码。因此,如果我有一个表格,其中我将颜色的名称记录到名为“color”的字段中,字段 id 为 19,并将示例图像记录到名为“sample image”的字段中,字段 id 为 21,我相信我的方法调用将类似于:

my_color_file = #base64 encoding of your file
my_fields = {'19': 'Seafoam Green'}
my_uploads = [{'field': 21, 'filename':'seafoam_green.png', 'value': my_color_file}]
client.add_record(fields=my_fields, uploads=my_uploads)

或者,如果您使用的是字段名称:

my_color_file = #base64 encoding of your file
my_fields = {'color': 'Seafoam Green'}
my_uploads = [{'field': 'sample_image', 'filename':'seafoam_green.png', 'value': my_color_file}]
client.add_record(fields=my_fields, named=True, uploads=my_uploads)

client只是您之前使用此模块具有的任何构造函数实例化的对象。

于 2016-07-23T17:50:43.680 回答