With `order_by()` you can order the results from database based on your choice of fields. You can provide a string with field name or list of strings with fields names. Ordering in sql will be applied in order of names you provide in order_by. By default i
(self, columns: Union[list, str, OrderAction])
| 619 | return self.fields(columns=columns, slot="exclude") |
| 620 | |
| 621 | def order_by(self, columns: Union[list, str, OrderAction]) -> "QuerySet[T]": |
| 622 | """ |
| 623 | With `order_by()` you can order the results from database based on your |
| 624 | choice of fields. |
| 625 | |
| 626 | You can provide a string with field name or list of strings with fields names. |
| 627 | |
| 628 | Ordering in sql will be applied in order of names you provide in order_by. |
| 629 | |
| 630 | By default if you do not provide ordering `ormar` explicitly orders by |
| 631 | all primary keys |
| 632 | |
| 633 | If you are sorting by nested models that causes that the result rows are |
| 634 | unsorted by the main model `ormar` will combine those children rows into |
| 635 | one main model. |
| 636 | |
| 637 | The main model will never duplicate in the result |
| 638 | |
| 639 | To order by main model field just provide a field name |
| 640 | |
| 641 | To sort on nested models separate field names with dunder '__'. |
| 642 | |
| 643 | You can sort this way across all relation types -> `ForeignKey`, |
| 644 | reverse virtual FK and `ManyToMany` fields. |
| 645 | |
| 646 | To sort in descending order provide a hyphen in front of the field name |
| 647 | |
| 648 | :param columns: columns by which models should be sorted |
| 649 | :type columns: Union[list, str] |
| 650 | :return: QuerySet |
| 651 | :rtype: QuerySet |
| 652 | """ |
| 653 | if not isinstance(columns, list): |
| 654 | columns = [columns] |
| 655 | |
| 656 | orders_by = [ |
| 657 | ( |
| 658 | OrderAction(order_str=x, model_cls=self.model_cls) # type: ignore |
| 659 | if not isinstance(x, OrderAction) |
| 660 | else x |
| 661 | ) |
| 662 | for x in columns |
| 663 | ] |
| 664 | |
| 665 | order_bys = self.order_bys + [x for x in orders_by if x not in self.order_bys] |
| 666 | return self.rebuild_self(order_bys=order_bys) |
| 667 | |
| 668 | async def values( |
| 669 | self, |