'How to filter with hybrid_property in sqlaclhemy model to decode value from column?

I can not understand how to work with hybrid properties or what are their limitations.
I have a model where columns stores domain names in ascii. Some of them are idna encoded represented like utf-8:

domain.com
xn--d1acufc.xn--p1ai # домен.рф
...

I want to filter domains with one interface. There is nothing hard with filtering by ASCII string, however non-ascii is different story. I want to pass substring like "рф" to filter_by_name() and get expected result - 1 record in thic case with xn--d1acufc.xn--p1ai

class SomeModel(Base):
    __tablename__ = "table"
    name = Column(String(255))

    @hybrid_property
    def decoded_name(self):
        return self.name.encode("utf-8").decode("idna")

    @decoded_name.expression
    def decoded_name(cls):
        return cls.name.encode("utf-8").decode("idna")

    @classmethod
    def filter_by_name(cls, substring: str):
        with sessionmaker.begin() as s:
            query = s.query(SomeModel)

            if substring.isascii():
                condition = cls.name.like(f"%{substring}%")
            else:
                condition = cls.decoded_name.like(f"%{substring}%")

            query = query.filter(condition)
        return query.all()

Buy I get error below. It seems that i should use sqlclhemy func with corresposnd to SQL functions but...what about if I need python routine like encode/decode?

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with SomeModel.name has an attribute 'encode'


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source