当前位置:首页 > Python教程 > python技巧

Python 20th Day

Model

  • 创建表

  •                     from django.db import models
       
    class userinfo(models.Model):
        name = models.CharField(max_length=30)
        email = models.EmailField()
        memo = models.TextField()

     

  • 基本操作
  •                     #
                        
                        #
    
                        #
                         models.Tb1.objects.create(c1=‘xx‘, c2=‘oo‘)  增加一条数据,可以接受字典类型数据 **kwargs
                        #
                         obj = models.Tb1(c1=‘xx‘, c2=‘oo‘)
                        #
                         obj.save()
                        #
                        
                        #
    
                        #
                         models.Tb1.objects.get(id=123)         # 获取单条数据,不存在则报错(不建议)
                        #
                         models.Tb1.objects.all()               # 获取全部
                        #
                         models.Tb1.objects.filter(name=‘seven‘) # 获取指定条件的数据
                        #
                        
                        #
    
                        #
                         models.Tb1.objects.filter(name=‘seven‘).delete() # 删除指定条件的数据
                        #
                        
                        #
                         models.Tb1.objects.filter(name=‘seven‘).update(gender=‘0‘)  # 将指定条件的数据更新,均支持 **kwargs
                        #
                         obj = models.Tb1.objects.get(id=1)
                        #
                         obj.c1 = ‘111‘
                        #
                         obj.save()                                                 # 修改单条数据
                    

     

  • 双下划线
    • 限定条件
    • # 获取个数#
      # models.Tb1.objects.filter(name=‘seven‘).count()# 大于,小于#
      # models.Tb1.objects.filter(id__gt=1)              # 获取id大于1的值# models.Tb1.objects.filter(id__lt=10)             # 获取id小于10的值# models.Tb1.objects.filter(id__lt=10, id__gt=1)   # 获取id大于1 且 小于10的值# in#
      # models.Tb1.objects.filter(id__in=[11, 22, 33])   # 获取id等于11、22、33的数据# models.Tb1.objects.exclude(id__in=[11, 22, 33])  # not in# contains#
      # models.Tb1.objects.filter(name__contains="ven")# models.Tb1.objects.filter(name__icontains="ven") # icontains大小写不敏感# models.Tb1.objects.exclude(name__icontains="ven")# range#
      # models.Tb1.objects.filter(id__range=[1, 2])   # 范围bettwen and# 其他类似#
      # startswith,istartswith, endswith, iendswith,# order by#
      # models.Tb1.objects.filter(name=‘seven‘).order_by(‘id‘)    # asc# models.Tb1.objects.filter(name=‘seven‘).order_by(‘-id‘)   # desc# limit 、offset#
      # models.Tb1.objects.all()[10:20]# group byfrom django.db.models import Count, Min, Max, Sum
          # models.Tb1.objects.filter(c1=1).values(‘id‘).annotate(c=Count(‘num‘))# SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"

       

    • 连表操作  

    • class Clothes(models.Model):
          color=models.ForeignKey("Colors")   #与颜色表为外键,颜色表为母表
          description=models.CharField(max_length=10) #描述def__str__(self):
              return self.description
      
      
      class Colors(models.Model):
          colors=models.CharField(max_length=10) #蓝色def__str__(self):
              return self.colors
      #颜色为红的服装,description都更新为大美女
      #写法1:
      models.Clothes.objects.filter(color__colors="").update(description="大美女")
      #写法2:
      models.Clothes.objects.filter(color_id=models.Colors.objects.get(colors="").id).update(description="大美女")
      #写法3:
      colors_obj=models.Colors.objects.get(colors="")
      colors_obj.clothes_set.filter(id__gte=1).update(description="大美女")
      #增添子表数据,形式与一对一一致
      #添加颜色为绿的服装:小帅哥
      #方法1:
      models.Clothes.objects.create(color=models.Colors.objects.get(colors="绿"),description="小帅哥")
      #方法1补充:
      models.Clothes.objects.create(color_id=models.Colors.objects.get(colors="绿").id,description="小帅哥")
      #方法2:
      c_obj=models.Clothes(color=models.Colors.objects.get(colors="绿"),description="小帅哥")
      c_obj.save()

       

原文:http://www.cnblogs.com/garyang/p/5901538.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐