zoukankan      html  css  js  c++  java
  • Django--模型

    对象关系映射(Object Relational Mapping,简称ORM)

    什么是ORM:面向对象和关系型数据库的一种映射

                        类----->表

                      对象--->数据行,记录

                      属性---->字段

    模型- 常用字段及参数:

          自增:  AutoField

              必须填入参数 primary_key=True 设置成数据库的主键

          整数类型:  IntegerField

              数值范围-2x10位-----2x10位

          布尔值:  BooleanField

          字符串类型:   CharField

                 必须提供 max_length 参数 设置字符的长度

          文本类型:  TextField

          日期类型:  DateField  (格式/YYYY-MM-DD)   和  DatetimeField  (格式/YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] )

                 参数:  auto_now_add    增加对象时自动添加日期时间

                      auto_now      每次修改和增加时自动添加或修改成当前时间

          小数类型:  DecimalField

                  参数:   max_digits  小数总长度

                       decimal_places 小数位长度

    常用的十三种查询方法:

        返回的结果是对象列表

            all( )  获取所有的数据  如:  ret=models.Person.objects.all()

            filter( )  获取所有满足条件的对象  如:  ret=models.Person.objects.filter(id=1)

            exclude( )  获取所有不满足条件的对象  如:  ret=models.Person.objects.exclude(id=1)

            order_by( )  排序 默认升序 -降序   如:  ret=models.Person.objects.all().order_by('age','id')

            reverse( )  给已经排好序的结果倒叙排列  如:  ret=models.Person.objects.all(().order_by('age','id').reverse()

            values( )  获取对象的字段名和值 不指定字段名,将获取所有字段的名和值  值的格式 [ {},{} ]  如:ret=models.Person.objects.all().values('name','id')  

            values_list( )  获取对象的值 不指定字段名,将获取所有字段的值 值的格式 [ (),() ]

            distinct( )  去重

        返回的结果是对象:

            get( )  获取一个对象  没有或多个时报错  如:ret=models.Person.objects.get(id=1)

            first( )   取第一个对象  如:  ret=models.Person.objects.filter(id=100).first()

            last( )   取最后一个对象  如:  ret=models.Person.objects.filter(id=100).last()

        返回的结果是布尔值:

            exists( )   判断数据是否存在    如:  ret=models.Person.objects.filter(id=1).exists()

        返回的结果是数字:

            count( )  计数   如:ret=models.Person.objects.all().count()

     filter的过滤集合功能:

          __gt  (大于)   __gte  (大于等于)  __lt  (小于)  __lte  (小于等于)  __range=[1,3]  (范围,查找所有的值,包括1和3)  __in=[1,3]  (查找1和3)  

          __year (进行精确的年匹配)

          __contains  (包含,含有该字段,区分大小写)  __icontains  (不区分大小写)

          __startswith  (以什么开头)  __istartswith  (以什么开头,不区分大小写)

          __endswith  (以什么结尾)  __iendswith  (以什么结尾,不区分大小写)

          

          如:  ret=models.Person.objects.filter(id__gt=1, id__lt=3)   

    外键的操作:

        正向查询:

          book_oj.publisher  关联的对象

          book_obj.publisher_id  关联的对象的id

          book_obj.publisher.id  关联的对象的id 比_id多一个步骤

        反向查询:

          不指定related_name

          pub_obj.book_set 管理对象

          指定related_name='books'

          pub_obj.books 管理对象

        基于字段查询

          指定related_name 用这个名字 如果在指定了related_query_name 则只能用这次指定的名字

    多对多操作,基于对象的查询:

        正向:author_obj.books.all()

        反向:

          不指定related_name    表名加set

          如: book_obj.author_set.all()

          指定related_name='authors'

          如: book_obj.authors.all()

      set  设置多对多关系

      author_obj.books.set([])

      author_obj.books.set([1,2,3])  要关联对象的ID  [对象的ID,对象的ID]

      author_obj.books.set(models.Book.objects.all())    要关联对象  [对象,对象]

      add  添加多对多的关系

      author_obj.books.add(1)   要关联的对象的id

      author_obj.books.add(models.Book.objects.get(id=2))   要关联的对象

      remove()  删除多对多的关系

      author_obj.books.remove(1)   要关联对象的id

      author_obj.books.remove(models.Book.objects.get(id=2))   要关联的对象

      clear() 清空当前对象的多对多关系

      author_obj.books.clear()

  • 相关阅读:
    周末之个人杂想(十三)
    PowerTip of the DaySorting Multiple Properties
    PowerTip of the DayCreate Remoting Solutions
    PowerTip of the DayAdd Help to Your Functions
    PowerTip of the DayAcessing Function Parameters by Type
    PowerTip of the DayReplace Text in Files
    PowerTip of the DayAdding Extra Information
    PowerTip of the DayPrinting Results
    Win7下IIS 7.5配置SSAS(2008)远程访问
    PowerTip of the DayOpening Current Folder in Explorer
  • 原文地址:https://www.cnblogs.com/leo-tail-x/p/10076751.html
Copyright © 2011-2022 走看看