• 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使用教程'}]>
    
  • 相关阅读:
    【Javascript】call
    【Android Studio】 资源下载
    【Javascript】数组之二
    【Java】Springboot集成Druid
    【Android Studio】Gradle
    【Postgres】根据字段数据创建空间字段
    【时空大数据】Access 到 Postgres 数据迁移遇到的ODBC坑----驱动程序和应用程序之间的体系结构不匹配
    【大数据】Hadoop单机安装配置
    【大数据】虚拟机免密登录
    【大数据】虚拟机网络配置-CentOS
  • 原文地址:https://www.cnblogs.com/heris/p/16545580.html
Copyright © 2020-2023  润新知