Comments (7)
What are you trying to do, exactly?
from peewee.
You might try using attr=
:
If we wish to, we can control where Peewee puts the joined User instance in the above query, by specifying an attr in the join() method:
>>> query = Tweet.select(Tweet.content, User.username).join(User, attr='author')
>>> for tweet in query:
... print(tweet.author.username, '->', tweet.content)
...
huey -> meow
huey -> hiss
huey -> purr
mickey -> woof
mickey -> whine
Via http://docs.peewee-orm.com/en/latest/peewee/relationships.html
from peewee.
class Transaction(Model):
one_customer_type = ForeignKeyField(
model=CustomerType, null=True,
)
another_customer_type = ForeignKeyField(
model=CustomerType, null=True,
)
class CustomerType(Model):
name = CharField(max_length=16, default='', null=True)
Transaction.select(
peewee.SQL('t2.name').alias('one_ct'),
peewee.SQL('t3.name').alias('another_ct'),
).join(
CustomerType,
on=CustomerType.id == Transaction.one_customer_type,
alias='t2'
).join(
CustomerType,
on=CustomerType.id == Transaction.another_customer_type,
alias='t3'
)
Does that make more sense?
from peewee.
ok, i just saw your response i will try attr. thx
from peewee.
i'm trying to use Union with the query as well so i 'm trying not to pull the whole object of CustomerType but only CustomerType.name with certain alias so the signature of two selects would be the same
from peewee.
OK, seems like i have figured it out.
class Transaction(Model):
one_customer_type = ForeignKeyField(
model=CustomerType, null=True,
)
another_customer_type = ForeignKeyField(
model=CustomerType, null=True,
)
class CustomerType(Model):
name = CharField(max_length=16, default='', null=True)
first_ct = CustomerType.alias('first_ct')
another_one_ct = CustomerType.alias('another_one_ct')
Transaction.select(
one_ct.name.alias('one_ct'),
another_one_ct.name.alias('another_ct'),
).join(
first_ct,
on=first_ct.id == Transaction.one_customer_type,
).join(
another_one_ct,
on=another_one_ct.id == Transaction.another_customer_type,
)
from peewee.
Yes, I wanted to ask why you were needing the aliases because I suspected you weren't aware of the APIs for selecting, joining and dealing with stuff like that via the query builder.
Using model class aliases is the correct approach and your last example looks like you're on the right track now.
from peewee.
Related Issues (20)
- Dynamic table name query in the API request HOT 2
- SqliteMigrator: Using sqlite3.version_info instead of sqlite3.sqlite_version_info HOT 3
- How to simply replace in postgres? HOT 1
- Incorporating pgvector for document/similarity search? HOT 1
- Wrong MySQL error HOT 1
- Adding Support for jsonb_array_elements in Peewee: Discussion and Contribution Proposal HOT 3
- Full path to alias? HOT 3
- ManyToMany Fields Model init hint many unnecessary query HOT 3
- Cython 3 support HOT 3
- Problematic rollback semantics with nested transactions HOT 9
- Selected relations not included in the final result. HOT 3
- bulk_update update the field with wrong value HOT 5
- 500 internal error on every .py page of Roxy-WI HOT 2
- Duplicate PKEY even when setting the id_seq HOT 16
- The query operator .between in https://docs.peewee-orm.com/en/latest/peewee/query_operators.html does not state if it includes boundaries or not HOT 2
- Property that does calculation DateTime + minutes HOT 1
- Add `__getitem__` to `ColumnFactory`
- Prefetch with union subquery HOT 3
- Clarification needed in peewee documentation query operators
- Addition fn.DATETIME HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peewee.