How do I specify an order for the records returned via a foreign key in Peewee?
I have the following (simplified) model;
class Order(peewee.Model):
order_no = peewee.CharField()
class OrderLine(peewee.Model):
order_item = peewee.CharField()
quantity = peewee.IntegerField()
price = peewee.DecimalField()
order = peewee.ForeignKeyField(Order)
When I fetch an order using my_order = Order.get(1234)
I would like to then iterate through the order lines in a specific order, sometimes in order_item
order, sometimes in quantity
order. Is there a way to do this? At the moment they seem to come back in order_item.id
order from the database.
The back-ref of the foreign-key (Order.orderline_set
) is just a wrapper around a select query, so you can order it how you wish:
for item in order.orderline_set.order_by(OrderLine.quantity):
...