【问题标题】:AttributeError: 'RelatedManager' object has no attribute 'remove'AttributeError:“RelatedManager”对象没有属性“删除”
【发布时间】:2018-02-21 00:29:02
【问题描述】:

在我的代码中某处说

publisher.publisherperson_set.remove(email__in=pp_remove_set)

执行此操作后,我得到了

AttributeError: 'RelatedManager' 对象没有属性 'remove'

确实:我查看了dir(publisher.publisherperson_set),它有很多操作(包括add),但没有remove

这在什么情况下可能?

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    不能是null

    documentation for RelatedManager.remove()

    对于ForeignKey 对象,此方法仅在null=True 时存在。 如果相关字段不能设置为NoneNULL),则 一个对象不能从一个关系中删除而不被添加到另一个对象中。

    很明显,仔细想想。
    我真正想做的是:

    publisher.publisherperson_set.filter(email__in=pp_remove_set).delete()
    

    【讨论】:

      【解决方案2】:

      自从我通过相关搜索来到这里后才发布此信息 AttributeError: 'RelatedManager' object has no attribute 'delete'

      我正在寻找的是:

      thing.stuff_set.all().delete()
      

      注意:仍在学习 django,但我假设任何查询集操作 filterallorder_byexcludeannotate

      可以使用。虽然我目前不确定从all()stuff_set 返回的查询集列表有什么细微差别:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-14
        相关资源
        最近更新 更多