【问题标题】:How to empty a many to many field in django如何在 django 中清空多对多字段
【发布时间】:2021-08-21 19:32:10
【问题描述】:

我想清空以下多对多字段中的所有内容(weekday_with_class):

 all_existing_class.weekday_with_class = None

这里是models.py:

   weekday_with_class = models.ManyToManyField('Weekday', blank=True, related_name='weekday_with_class')

但是,如果我执行上面的代码,我会得到这个错误:

Direct assignment to the forward side of a many-to-many set is prohibited. Use weekday_with_class.set() instead.

对于一个实例,如何基本上使我的weekday_with_class 为空?谢谢,如果您需要更多信息,请发表评论。

【问题讨论】:

标签: python django django-models many-to-many django-errors


【解决方案1】:
all_existing_class.weekday_with_class.clear()

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 2011-02-01
    • 2013-04-27
    • 2010-11-14
    相关资源
    最近更新 更多