Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用对象属性的SQLAlchemy动态查询_Python_Sqlalchemy - Fatal编程技术网

Python 使用对象属性的SQLAlchemy动态查询

Python 使用对象属性的SQLAlchemy动态查询,python,sqlalchemy,Python,Sqlalchemy,我希望动态查询对象的属性。我不知道在执行时将使用哪个属性或列 class Product(Base): __tablename__ = 'products' sku = Column(String, primary_key=True) list_price = Column(String) status = Column(String) url = Column(String) special_price1 = Column(String)

我希望动态查询对象的属性。我不知道在执行时将使用哪个属性或列

class Product(Base):
    __tablename__ = 'products'

    sku = Column(String, primary_key=True)
    list_price = Column(String)
    status = Column(String)
    url = Column(String)
    special_price1 = Column(String)
    special_price2 = Column(String)
    special_price3 = Column(String)
我有一个SQLAlchemy基类
Product
,它描述了一些属性,以及与标价不同的其他特殊价格

然后我有一个下面的
PriceList
类,它可以访问额外的资源和方法,帮助报告和更新
'products'
表中的列。此类存储有关所有
产品
对象的唯一特殊价目表的信息

class PriceList:

    def __init__(self, name, db_col_name):
        # Display name
        self.name = name

        # Used for querying the database for a specific column
        # This will always be one of the 4 price related column names
        # list_price, special_price1, special_price2, or special_price3
        self.db_col_name = db_col_name
我随后开始迭代每个
产品
价格表
实例

for product in products:
    for price_list in price_lists:
        # Do stuff
此时,我的
产品
对象有一个新的特殊价格,或多个新的特殊价格,我计划在数据库中更新。我可以简单地将我的对象添加到数据库会话并提交,但在提交之前,我需要获取旧的价格并将它们链接到各自的价格列表。旧的价格在一份报告中使用,该报告后来通过电子邮件发送给我。我现在正在做的事情如下

for product in products:
    sku = product.sku
    for price_list in price_lists:
        # New price
        new_price = product.__getattribute__(price_list.db_col_name)

        # Get the existing special price from the database
        old_price = s.query(Product.__getattribute__(Product, price_list.db_col_name)).filter(Product.sku.like(sku)).first()

我觉得我使用uu getattribute_uuu()使这件事复杂化了很多。它是有效的,但这看起来不像是蟒蛇。有人知道在更新之前获取未知列值的更好方法吗?数据库更新每~500个产品只发生一次或两次,因此在处理每个特殊价格时将其存储在外部变量中并不十分有效。

要动态访问属性,应使用内置的

如果实例已过时,则应使用,这意味着下次访问属性时,将从数据库中检索这些属性

s.expire(product)

# or only expire price
s.expire(product, [price_list.db_col_name])

要动态访问属性,应该使用内置的

如果实例已过时,则应使用,这意味着下次访问属性时,将从数据库中检索这些属性

s.expire(product)

# or only expire price
s.expire(product, [price_list.db_col_name])

如果产品对象就在那里,为什么要重新计算价格?产品对象的价格已经改变了这个点。它有一个不同于数据库的新价格。1。可以在刷新之前访问属性的加载值。2.即使不能保存,也可以自己保存属性。如果产品对象就在那里,为什么要重新蚀刻价格?产品对象的价格已经更改了该点。它有一个不同于数据库的新价格。1。可以在刷新之前访问属性的加载值。2.即使你不能,你也可以自己保存属性。谢谢你的回答!我的问题是独特的,令人困惑的,但你给了我足够的理由继续下去
getattr()
是用于此场景的正确工具。在查询时我感到困惑,因为
getattr()
返回该属性的值。当运行
s.query(Object.attribute)
时,我一直认为参数不是一个值,而是一个基于类的指针。。。谢谢你改掉这个坏习惯。我也不知道会话。过期,很高兴知道!谢谢你的回答!我的问题是独特的,令人困惑的,但你给了我足够的理由继续下去
getattr()
是用于此场景的正确工具。在查询时我感到困惑,因为
getattr()
返回该属性的值。当运行
s.query(Object.attribute)
时,我一直认为参数不是一个值,而是一个基于类的指针。。。谢谢你改掉这个坏习惯。我也不知道会话。过期,很高兴知道!