I have a property (parsing
in the sample below) that return a dictionary.
I want to be able to filter query results based on a value inside that dictionary.
I tried to follow this response.
I have the following (simplified) code:
class Document(Base):
@property
def parsing(self) -> Optional[dict]:
# very simplified code, this involve more python code and can't be easily a sqlalchemy expression
if not self.a_dictionary:
return None
return self.a_dictionary
@hybrid_method
def get_info(self) -> Optional[str]:
if not self.parsing:
return None
return self.parsing["info"]
@get_info.expression
def get_info(self) -> String:
return cast(self.parsing["info"], String)
But when I try to use the expression in a query, something like:
query = Document.query(...).filter(Document.get_info().in_(info_list))
I get that error: 'property' object is not subscriptable
caused by the f.parsing["info"]
bit.
Is hybrid property and expression really what I should use ? How to fix that?