Skip to main content
 首页 » 编程设计

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

2025年05月04日14myhome

我有以下冗长的代码,它设置 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}) 

请您参考如下方法:

您可以使用 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 关系,您将需要对这些值进行一些预处理,因此

  • 处理后从参数字典中删除原始值
  • 使用正确的键将处理后的值添加到参数字典中

构建参数字典后,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)