【问题标题】:Django query find whether a column has None valueDjango查询查找列是否具有None值
【发布时间】:2012-08-19 19:04:27
【问题描述】:

我在内容表中有以下列作为名称和标签。

从下面的 django 查询中是否有任何方法可以在不使用 for 循环而不使用 .exclude() 的情况下找到名为 column 的标签具有 None

  newobj = Content.objects.filter(name="Harry")
  boolval = if tag has or not not None value

【问题讨论】:

    标签: django django-models django-views django-queryset


    【解决方案1】:

    是的,使用 __isnull[0]

    Content.objects.filter(name="Harry", tag__isnull=True)
    

    [0]https://docs.djangoproject.com/en/dev/ref/models/querysets/#isnull

    对此进行扩展:

    我们不能从 newobj 中做到这一点 - Rajeev

    .filter() 返回一个查询集。

    newobj = Content.objects.filter(name="Harry")
    boolval = newobj.exists(tag__isnull=True)
    

    如果有带有空标签的 Harry,这将返回 true/false(通过存在)。

    【讨论】:

    • 我们不能从 newobj 做到这一点
    • 查看扩展版本。我希望我正确理解了您的评论。
    猜你喜欢
    • 2011-08-06
    • 2020-06-09
    • 1970-01-01
    • 2018-08-22
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    相关资源
    最近更新 更多