Peewee Tutorial
Selected Reading
- Peewee - Discussion
- Peewee - Useful Resources
- Peewee - Quick Guide
- Peewee - Using CockroachDB
- Peewee - PostgreSQL & MySQL Extensions
- Peewee - SQLite Extensions
- Peewee - Integration with Web Frameworks
- Peewee - Query Builder
- Peewee - Database Errors
- Peewee - Atomic Transactions
- Peewee - User defined Operators
- Peewee - Retrieving Row Tuples/Dictionaries
- Peewee - SQL Functions
- Peewee - Counting & Aggregation
- Peewee - Sorting
- Peewee - Subqueries
- Peewee - Relationships & Joins
- Peewee - Connection Management
- Peewee - Defining Database Dynamically
- Peewee - Using PostgreSQL
- Peewee - Using MySQL
- Peewee - Constraints
- Peewee - Create Index
- Peewee - Delete Records
- Peewee - Update Existing Records
- Peewee - Primary & Composite Keys
- Peewee - Filters
- Peewee - Select Records
- Peewee - Insert a New Record
- Peewee - Field Class
- Peewee - Model
- Peewee - Database Class
- Peewee - Overview
- Peewee - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Peewee - Sorting
Peewee - Sorting
It is possible to select records from a table using order_by clause along with model’s select() method. Additionally, by attaching desc() to the field attribute on which sorting is to be performed, records will be collected in descending order.
Example
Following code display records from contact table in ascending order of City names.
rows=Contacts.select().order_by(Contacts.City) print ("Contact pst in order of city") for row in rows: print ("RollNo:{} Name: {} City:{}".format(row.RollNo,row.Name, row.City))
Output
Here is the sorted pst which is arranged according to ascending order of city name.
Contact pst in order of city RollNo:107 Name: Beena City:Chennai RollNo:102 Name: Amar City:Delhi RollNo:108 Name: John City:Delhi RollNo:103 Name: Raam City:Indore RollNo:101 Name: Anil City:Mumbai RollNo:106 Name: Hema City:Nagpur RollNo:104 Name: Leena City:Nasik RollNo:109 Name: Jaya City:Nasik RollNo:110 Name: Raja City:Nasik RollNo:105 Name: Keshav City:Pune
Example
Following code displays pst in descending order of Name field.
rows=Contacts.select().order_by(Contacts.Name.desc()) print ("Contact pst in descending order of Name") for row in rows: print ("RollNo:{} Name: {} City:{}".format(row.RollNo,row.Name, row.City))
Output
The output is as follows −
Contact pst in descending order of Name RollNo:110 Name: Raja City:Nasik RollNo:103 Name: Raam City:Indore RollNo:104 Name: Leena City:Nasik RollNo:105 Name: Keshav City:Pune RollNo:108 Name: John City:Delhi RollNo:109 Name: Jaya City:Nasik RollNo:106 Name: Hema City:Nagpur RollNo:107 Name: Beena City:Chennai RollNo:101 Name: Anil City:Mumbai RollNo:102 Name: Amar City:DelhiAdvertisements