如何清除multtomany的所有关系? [英] How to remove all relations from manytomany?

查看:105
本文介绍了如何清除multtomany的所有关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个模型中,我有:

class MyModel(models.Model):
    relations = models.ManyToManyField(OtherModel)
    ....

如果我选择:

my_object.relations.remove(other_model_object)

它的工作。

如何从关系中删除所有对象? my_object.relations.clean()不起作用。

How to remove all objects from the relations? my_object.relations.clean() is not working.

推荐答案

首先,您需要清除关系使用.clear()或.remove(),更适合您的需求根据文档

First, you'll need to clear the relationship(s) by using .clear() or .remove(), whichever suits your needs better according to the docs.

之后,您需要使用[YourModel]删除对象]。删除()方法。

After that, you'll need to delete the object(s) by using the [YourModel].delete() method.

这篇关于如何清除multtomany的所有关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆