Creating an sqlalchemy hybrid_property that allows for "in_" operator

تعرفه تبلیغات در سایت

آخرین مطالب

امکانات وب

Vote count: 0

How would you make a hybrid_property that allows the in_ clause? What would the SQLAlchemy expression look like?

class Version(Base):
    version_id = Column(Integer, primary_key=True)
    package_id = Column(Integer, ForeignKey('package.package_id')
    version = Column(String(32), index=True)

@hybrid_property
def pkg_id_concat_vers(self):
    # "23~~0.2.4"
    return "{}~~{}".format(self.package_id, self.version)

# TODO
#@pkg_id_concat_vers(self):
#    pass

# Using the naive `(not) in` doesn't actually apply the filter
data = ['23~~0.2.4', '57~~0.0.1']
result = (session.query(Version)
          .filter(Version.pkg_id_concat_vers in data)
          ).all()

# Using `in_()` throws an Attribute Error, as it's a string.
data = ['23~~0.2.4', '57~~0.0.1']
result = (session.query(Version)
          .filter(Version.pkg_id_concat_vers.in_(data))
          ).all()
asked 1 min ago
dthor

نویسنده : استخدام کار بازدید : 8 تاريخ : سه شنبه 4 ارديبهشت 1397 ساعت: 5:28

فهرست وبلاگ