【问题标题】:Filtering on foreign key null doesn't work in Django 1.7过滤外键空值在 Django 1.7 中不起作用
【发布时间】:2015-02-15 16:00:20
【问题描述】:

这是模型设置和我正在尝试的查询的简要示例。这在 Django 1.6 中没有给我任何错误,但在 1.7 中它给了我一个例外。似乎接口查询集上没有“隐式”外键。我尝试了 select_related('gwportprefix') 和 prefetch_related('gwportprefix') 都没有运气。

class Interface(models.Model):
    # Several fields here that are not related.
    pass

class GwPortPrefix(models.Model):
    interface = models.ForeignKey('Interface', db_column='interfaceid')

for gwport in Interface.objects.filter(gwportprefix__isnull=False):
    gwport_matches.add(gwport)

这会产生以下 FieldError:

无法将关键字“gwportprefix”解析为字段。选择是(直接在接口上的所有内容。没有隐式外键)

【问题讨论】:

    标签: python django django-models


    【解决方案1】:

    在 django 1.7 中,QuerySet / Manager 实现发生了许多变化。你试过了吗?

    Interface.objects.filter(gwportprefix_set__isnull=False)
    

    【讨论】:

    • 用关键字“gwportprefix_set”给我同样的例外。
    猜你喜欢
    • 2015-11-04
    • 2016-07-12
    • 1970-01-01
    • 2020-05-29
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多