在flask中有没有办法获取request.json中的每个对象并保存模型上的每个属性?

时间:2015-10-05 17:52:46

标签: python flask refactoring flask-sqlalchemy params

我有以下详细代码,用于设置request.json['example']中的每个变量,然后保存模型Inventory。有没有办法用更少的代码来做到这一点?例如在ruby中我可以简单地将参数传递给Inventory(inventory_params)

@api.route('/new/<id>', methods=['POST'])
@cross_origin()
def new_item(id):
    date_opened = None
    date_expired = None
    received_date = None
    order_date = None
    if len(request.json['opened']) >= 1:
        date_opened = datetime.strptime(request.json['opened'], '%m/%d/%Y')
        print(date_opened)
    if len(request.json['expiration_date']) >= 1:
        date_expired = datetime.strptime(request.json['expiration_date'], '%m/%d/%Y')
    if len(request.json['received_date']) >= 1:
        received_date = datetime.strptime(request.json['received_date'], '%m/%d/%Y')
    if len(request.json['order_date']) >= 1:
        order_date = datetime.strptime(request.json['order_date'], '%m/%d/%Y')
    amount=request.json['amount']
    units=request.json['units']
    location=request.json['location']
    opened=request.json['opened']
    lot=request.json['lot']
    batch=request.json['batch']
    notes=request.json['notes']
    price=request.json['price']
    country_code=request.json['country_code']
    secondary_location=request.json['secondary_location']
    vendor_code=request.json['vendor']
    uid = request.json['uid']
    item = Inventory(vendor_code=vendor_code, country_code=country_code, price=price, secondary_location=secondary_location, lot=lot, original_amount=amount, amount=amount, original_units=units, units=units, location=location, plant_material_id=id, uid=uid, batch=batch, notes=notes, date_opened=date_opened, date_expired=date_expired, received_date=received_date, order_date=order_date )
    db.session.add(item)
    db.session.commit()
    return jsonify({'results': True})

1 个答案:

答案 0 :(得分:1)

您可以使用argument unpacking

基本原则是,如果你有一个接受关键字参数的函数或方法,比如

def foo(bar=42, qux=None):
    pass

你可以而不是像foo(bar=1, qux=23)那样调用它来从字典中获取这些参数并通过解包将它们传递给函数(这就是**语法的作用):

kwargs = {'bar': 1, 'qux': 23}

foo(**kwargs)

因为在您的情况下,requests.json中的键/值与Inventory的构造函数所需的参数之间没有严格的1:1关系,您需要进行一些预处理那些价值观,因此

  • 在处理完毕后从参数dict中删除原始值
  • 使用正确的密钥
  • 将处理后的值添加到参数dict中

在构建参数字典后,Inventory(**params)会将所有这些参数传递给Inventory.__init__(),因此您需要确保匹配方法的签名。

这是一个自包含的示例(没有Flask或SQLAlchemy,但它确实没有区别):

from datetime import datetime


params = {
    'opened': '05/07/2015',
    'vendor_code': 'VC',
    'country_code': 'US',
}


class Inventory(object):
    def __init__(self, vendor_code=None, country_code=None, date_opened=None):
        self.vendor_code = vendor_code
        self.country_code = country_code
        self.date_opened = date_opened

    def __repr__(self):
        return repr(self.__dict__)


def create_inventory(params):
    # Deal with a raw value that needs to be processed
    date_opened = None
    if len(params['opened']) >= 1:
        date_opened = datetime.strptime(params['opened'], '%m/%d/%Y')

    # Remove the raw value from the params dict
    params.pop('opened')

    # Add the processed value to the `params` dict with the proper key
    params['date_opened'] = date_opened

    # All the other parameters get passed through unmodified
    inv = Inventory(**params)
    return inv

print create_inventory(params)