zoukankan      html  css  js  c++  java
  • 069:ORM查询条件详解-isnull和regex

    ORM查询条件详解-isnull和regex:

    isnull:
    根据值是否为空进行查找。示例代码如下:

    articles = Article.objects.filter(create_time__isnull=True)

    以上的代码的意思是获取所有发布日期不为空的文章。将来翻译成 SQL 语句如下:

    SELECT `article`.`id`, `article`.`title`, `article`.`content`, `article`.`category_id`, `article`.`create_time` FROM `article` WHERE `article`.`create_time` IS NUL

    regex和iregex:

    大小写敏感和大小写不敏感的正则表达式。示例代码如下:

    articles = Article.objects.filter(title__iregex=r"^fuck")

    以上代码的意思是提取所有标题以 hello 字符串开头的文章。将翻译成以下的 SQL 语句:

    SELECT `article`.`id`, `article`.`title`, `article`.`content`, `article`.`category_id`, `article`.`create_time` FROM `article` WHERE `article`.`title` REGEXP ^fuck

    iregex 是大小写不敏感的。

    实例截图如下:

     根据关联的表进行查询:

    假如现在有两个 ORM 模型,一个是 Article ,一个是 Category 。代码如下:

    class Category(models.Model):
      """文章分类表"""
      name = models.CharField(max_length=100)
    class Article(models.Model):   """文章表"""   title = models.CharField(max_length=100,null=True)   category = models.ForeignKey("Category",on_delete=models.CASCADE)

    比如想要获取文章标题中包含"hello"的所有的分类。那么可以通过以下代码来实:

    categories = Category.object.filter(article__title__contains("hello"))
  • 相关阅读:
    生成html报告并整合自动发动邮件功能
    python--selenium多线程执行用例实例/执行多个用例
    python--selenium实用的自动生成测试HTML报告方法--HTMLTestRunner
    mysql完整版
    hibernate分页
    解决hibernate向mysql插入中文乱码问题
    c++语言的 代码组织
    命令总结
    c++ 命名空间
    dpkg 、apt
  • 原文地址:https://www.cnblogs.com/zheng-weimin/p/10238380.html
Copyright © 2011-2022 走看看