CSIC_716_2020107【Django入门---模型层】

单表查询

时间格式的字段

publish_time = models.DateField()  # 年月日
publish_time = models.DateTimeField()  # 年月日 时分秒
    """
    auto_now:每次修改数据的时候 都会自动将最新的更新时间记录下来
    auto_now_add:只在创建数据的时候将创建时间自动记录下来 之后不会自动改变
    """

用法示例
publish_time = models.DateField(auto_now=True)  
publish_time = models.DateTimeField(auto_now_add=True)  

  

 必知必会16条
        1.create()  返回值:对象本身
        2.all()     返回值:queryset对象
        3.filter()  返回值:queryset对象
        4.update()   返回值:影响的行数
        5.delete()   返回值:影响的行数
        6.first()   返回值:queryset结果中的对象
        7.last()    返回值:queryset结果中的对象
        8.get()    返回值:对象本身,如果get的对象不存在,就报错
        9.values('field')   返回值:queryset对象,对象中包着{field:value}字典
        10.values_list('field')  返回值:queryset对象,对象中包着(value,)元组
        11.order_by()  返回值:queryset对象; order_by('name')升序;和order_by('-name')降序
        12.count()    返回值:整型,统计queryset中元素的数量
        13.exclude()   返回值:queryset对象
        14.exists()     返回值:布尔型, 用于判断能否根据条件查出queryset对象
        15.reverse()   返回值:queryset对象 ,order_by('name').reverse()等价于order_by('-name')
        16.distinct()  返回值:queryset对象, 去重务必要保证value()中所列的字段完全一致,才能去重

  

双下划线查询

****************大于(等于)*******
__gt=
__lt=
__gte=  # greater than equal
__lte=
****************范围******* __in=[ ,,,] __range=( ,) #包含边界
***************模糊查询******* __contains=‘F’ #严格大小写 __icontains=‘F’ #忽略大小写 ignorecontains

  

ding

多表查询

聚合查询

分组查询

F与Q查询

猜你喜欢

转载自www.cnblogs.com/csic716/p/12168678.html