zoukankan      html  css  js  c++  java
  • 11)django-ORM(操作增删改查)

    ORM从增删改查等方面说明

    一:创建数据

      

        #创建数据两种方式1,推荐方式1
        UserInfo.objects.create(username="root",password="123")    
        
        #方式1变种
        
        user_dict={"username":"root","password":"123"}
        UserInfo.objects.create(**user_dict)
        
        #方式2
        obj=UserInfo(username="root2",password="123")
        obj.save()

    二:查询数据

        result=UserInfo.objects.all()#返回的是QuerySet类型,其实就是个UserInfo对象列表
        for row in result:
            print(row.username,row.password)
        result1=UserInfo.objects.filter(username="root")

    三:删除数据

      

    UserInfo.objects.filter(username="root").delete()

    四:更新数据

    UserInfo.objects.filter(username="root").update(username="roo2")

     五:元数据

        class User(models.Model):
            name=models.CharField(max_length,index=True)
            email=model.CharField(max_length,index=True)
            class meta:
                db_table="table_name"#数据库中生成的表名称 默认 app名称 + 下划线 + 类名
                
    
                #联合索引
                index_together=[
                    ("name","email"),
                    #联合索引支持最左前缀模式(上面单独索引可以不用建立,但是要考虑使用场景。因为单独最后一个字段查询是不能命中)
                    #select * from where name="xx" 可以命中索引
                    #select * from where name="xx" and emial="XX"可以命中索引
                    #select * from where emial="XX" 不能命中索引
                    #所以这是有代价,最后一个不能命中索引
                ]
                #联合唯一索引,和联合索引又加了限制唯一
                unique_together=[()]
    
                #admin中显示表的名称
                verbose_name
    
                #verbose_name加s
                verbose_name_plural
    
                verbose_name_plural="上课记录" django-admin中显示的是:上课记录s
  • 相关阅读:
    php date 时间差
    array_merge 和 + 号的的区别
    apache 添加https后导致http无法访问
    php 获取url
    TP5 事务处理
    LeetCode 每日一题 (盛最多水的容器)
    LeetCode 每日一题 (字符串转换整数 (atoi))
    LeetCode 每日一题(5. 最长回文子串)
    LeetCode 每日一题 (3 无重复字符的最长子串)
    LeetCode 每日一题 (两数相加)
  • 原文地址:https://www.cnblogs.com/lixiang1013/p/7765299.html
Copyright © 2011-2022 走看看