zoukankan      html  css  js  c++  java
  • django ORM 使用

    举例:表名称为case

    进入命令行orm 查询

    1:python manage.py shell

    2:from apps.models import *

    1:查询该表所以信息 all函数:

      case.objects.all();

    2:筛选表中字段信息,get 函数,

      1:根据test_name查询数据

      case_api.objects.get(test_name='用例名称')

      类似sql语句中where 操作

          2:更加id 查询test_name

      ORM语句:case_api.objects.filter(id="1").values("test_name")

           原始SQL:SELECT "apps_case_api"."test_name" FROM "apps_case_api" WHERE "apps_case_api"."id" = 1

    3:筛选表中字段信息,filter函数,

      case_api.objects.filter(test_name='用例名称')

      类似sql语句中where 操作

    4:更新表中数据,filter函数,

     根据test_name更新test_name

     把test_name=测试用例5' 的数据 更新test_name=用例名称5"

     1:根据test_name更新test_name

      *根据test_name=用例名称9 的数据 更新成test_name=用例名称1"  

        orm语句:case_api.objects.filter(test_name="用例名称9").update(test_name="用例名称1")

       原始sql语句:UPDATE "apps_case_api" SET "test_name" = '用例名称1' WHERE "apps_case_api"."test_name" = '用例名称9';

     2:根据iD更新test_name

      *根据d=8的数据,更新成 test_name="用例名称3"

      ORM语句:case_api.objects.filter(id="3").update(test_name="用例名称3")

      原始sql语句: UPDATE "apps_case_api" SET "test_name" = '用例名称3' WHERE "apps_case_api"."id" = 3; 

    5:查询表中该字段所有信息

         查询test_name所以数据

        ORM语句:case_api.objects.values("test_name")

     原始SQL:SELECT "apps_case_api"."test_name" FROM "apps_case_api" LIMIT 21;

    注意事项:get 和f ilter 的区别 

    get:

    1:get 的参数只能是model中定义的那些字段,只支持严格匹配 ,如果查询的信息不存在会抛出异常

    2:get 返回值是一个定义的model对象

    3:get的查询字段必须是主键或者唯一约束的字段。当返回多条记录或者是没有找到记录的时候都会抛出异常

    filter: 

    1:filter 查询支持模糊匹配如果查询的值不存在,不会像get抛出异常,而是会返回一个空的对象列表

    2:filter 返回值是一个对象列表

  • 相关阅读:
    SQL中 decode()函数简介
    php中foreach()的用法
    swfuploadphp上传说明
    未知,等知道什么以后再改
    php上传文件处理
    smarty中的section和foreach
    asp.net 异常:"DataBinding: 'System.Data.DataRowView'
    自定义MembershipProvider配合Asp.net 2.0 Login控件(转的,忘记哪里的了)
    地理信息中各种坐标系区别和转换总结
    asp.net异常DataRowView The type or namespace name 'DataRowView' could not be found
  • 原文地址:https://www.cnblogs.com/mahaining/p/11031835.html
Copyright © 2011-2022 走看看