Combining fields with different names using peewee

I use peewee as an ORM and has two classes:

class A(Model): name = CharField() body = TextField() class B(Model): title = CharField() body = TextField() 

I would like to get all entries from A and B , whose title / name begins with some characters, such as 'abc' . According to the documentation, the operator | should help, but I can't even perform the resulting Expression . Obviously, I would like to have the expression UNION and AS backstage. How can I get this through peewee?

+8
python sql orm peewee
source share
1 answer

You should be able to get the result you want with

 result = ( A().select(A.name.alias('name_title'), A.body).where(A.name == 'abc') | B().select(B.title.alias('name_title'), B.body).where(B.title == 'abc') ).select().execute() 

or

 search_text = 'abc' table_a_results = A().select( A.name.alias('name_title'), A.body ).where(A.name == search_text) table_b_results = B().select( B.name.alias('name_title'), B.body ).where(B.title == search_text) result = ( table_a_results | table_b_results ).select().execute() 

The .alias() method allows you to use AS functions according to docs

+4
source share

All Articles