Giter Club home page Giter Club logo

Comments (7)

coleifer avatar coleifer commented on June 4, 2024

What are you trying to do, exactly?

from peewee.

coleifer avatar coleifer commented on June 4, 2024

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.

AntonKyrychekCrit avatar AntonKyrychekCrit commented on June 4, 2024
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.

AntonKyrychekCrit avatar AntonKyrychekCrit commented on June 4, 2024

ok, i just saw your response i will try attr. thx

from peewee.

AntonKyrychekCrit avatar AntonKyrychekCrit commented on June 4, 2024

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.

AntonKyrychekCrit avatar AntonKyrychekCrit commented on June 4, 2024

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.

coleifer avatar coleifer commented on June 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.