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使用教程'}]>
写入自己的博客中才能记得长久
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!