Answer the question
In order to leave comments, you need to log in
What is the correct way to query/restrict multiple relationships in Peewee?
Let's say we have the following simple data structure:
Company(BaseModel):
id = AutoField()
# ...
User(BaseModel):
id = AutoField()
# ...
Delivery(BaseModel):
id = AutoField()
# peewee игнорирует lazy_load=True ?
manager = ForeignKeyField(User, backref='deliveries', lazy_load=True)
sender = ForeignKeyField(Company, backref='sender_deliveries')
receiver = ForeignKeyField(Company, backref='receiver_deliveries')
select = Delivery.select().limit(3)
select = Delivery.select(Delivery.id, ..., Delivery.sender).limit(3)
Sender = Company.alias()
Receiver = Company.alias()
select = Delivery.select(Delivery.id, Sender, Receiver, ...).limit(3)
select = select.join(Sender, JOIN.LEFT_OUTER, on=(Sender.id == Delivery.sender))
select = select.join(Receiver, JOIN.LEFT_OUTER, on=(Receiver.id == Delivery.receiver))
Answer the question
In order to leave comments, you need to log in
Actually, there are no other options, there is only one more or less effective way to limit the selection, so as not to sacrifice the selection id IN
and not pile up columns, implement a method to get columns, for example:
class BaseModel(Model):
class Meta:
database = database
@classmethod
def fields(cls):
fields = list()
for field in cls._meta.sorted_fields:
if not isinstance(field, ForeignKeyField):
fields.append(field)
return fields
select = Delivery.select(Delivery.sender, *Delivery.fields()).limit(3)
Delivery.sender
it will be "duplicated" and other minor inconveniences. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question