python orm group by
时间: 2023-12-27 19:24:17 浏览: 228
python实现orm
以下是两种使用Python ORM进行Group By操作的例子:
1. 使用原生SQL语句进行Group By操作:
```python
book_list = Book.objects.raw('SELECT * FROM books_book GROUP BY authors')
```
2. 使用Django自带的ORM接口进行Group By操作:
```python
from django.db.models import Sum
result = Book.objects.values("authors").annotate(total_pages=Sum("pages")).all()
```
阅读全文