django orm values别名

# 取别名方法一
a1 = Book.objects.filter().extra(select={'title2': 'title', 'category2': 'category'}).values("title2",  "category2")
print('这是a1',a1)

# 取别名方二
from django.db.models import F
a2 = Book.objects.filter(title="Alex的python使用教程").annotate(new_title=F('title')).values('new_title').order_by("new_title")
print('这是a2', a2)

输出:这是a1 <QuerySet [{'title2': 'python 编程指导', 'category2': 2}, {'title2': 'python的使敏感信用教程', 'category2': 1}, {'title2': 'go的过去025', 'category2': 3}, {'title2': 'Alex的使用教程123', 'category2': 1}, {'title2': 'Alex的使用教程', 'category2': 1}, {'title2': 'Alex的使用教程258', 'category2': 1}, {'title2': 'python 编程指导25', 'category2': 2}, {'title2': 'Alex的使用教程020', 'category2': 1}, {'title2': '深夜的深圳11', 'category2': 1}, {'title2': '深夜的深圳11', 'category2': 1}, {'title2': '北京沙河', 'category2': 2}, {'title2': '北京沙河', 'category2': 1}, {'title2': 'python 编程指导25', 'category2': 2}, {'title2': 'python 编程指导25', 'category2': 2}, {'title2': 'python 编程指导25', 'category2': 2}, {'title2': 'Alex的使用教程258', 'category2': 1}, {'title2': 'python 编程指导25', 'category2': 2}, {'title2': 'Alex的使用教程020', 'category2': 1}, {'title2': '深夜的深圳11', 'category2': 1}, {'title2': '深夜的深圳11', 'category2': 1}, '...(remaining elements truncated)...']>
这是a2 <QuerySet [{'new_title': 'Alex的python使用教程'}, {'new_title': 'Alex的python使用教程'}, {'new_title': 'Alex的python使用教程'}]>
posted @ 2022-08-02 23:36  ty1539  阅读(587)  评论(0编辑  收藏  举报