Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

django - How to remove all relations from manytomany?

In one model, I have this:

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

and if I choose:

my_object.relations.remove(other_model_object)

it works.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...