代码之家  ›  专栏  ›  技术社区  ›  user469652

Django:如何匹配数据库中的null

  •  0
  • user469652  · 技术社区  · 15 年前

    通过使用django ORM:如何匹配数据库中的结果不为空?

    example_objects = Example.objects.filter(field !=null)
    

    我不知道如何编写过滤器来获取所有非空记录。

    4 回复  |  直到 15 年前
        1
  •  4
  •   Jarret Hardie    15 年前

    我想你要找的是:

    Example.objects.filter(field__isnull=False)
    

    或:

    Example.objects.exclude(field__isnull=True)
    
        2
  •  2
  •   Daniel Roseman    15 年前
    example_objects = Example.objects.exclude(field=None)
    
        3
  •  0
  •   JonC    15 年前

    怎么样

    example_objects = Example.objects.exclude(field =null) ?

        4
  •  -1
  •   swastik    12 年前

    使用nullbooleanfield如下:

    happy = models.NullBooleanField(null=True, blank=True)