【问题标题】:How to remove all relations from manytomany?如何从多对多关系中删除所有关系?
【发布时间】:2011-08-29 12:47:54
【问题描述】:

在一个模型中,我有这个:

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

如果我选择:

my_object.relations.remove(other_model_object)

它有效。

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

【问题讨论】:

标签: django


【解决方案1】:

首先,您需要使用 .clear() 或 .remove() 清除关系,以更适合您的需要 according to the docs。

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

【讨论】:

  • 对于 m2m 字段 .remove() 将使用 QuerySet.delete() 删除关系。这应该删除我想的相关对象。如果我错了,请纠正我。
  • @sgauri 我感觉不是 100% 有信心,但我观察到的行为是,在调用 remove 时,只删除了“通过”记录,而 other_model_object 没有被删除。我认为remove 的文档并没有像clear 那样具体说明other_model_object 是否被删除。
【解决方案2】:

如果您只需要删除 2 个模型之间所有实例的关系,则可以通过访问关系表的管理器来执行此操作。 m2m 关系表可以通过MyModel.relations.through 访问,因此删除关系变得很容易:

MyModel.relations.through.objects.all().delete()

参考:

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.through

【讨论】:

  • 注意这一点,因为这将删除 m2m 表中的所有行,因为调用 through.objects 将 m2m 与 MyModel 解耦。所以你需要做类似Author.books.through.objects.filter(author_id=author.pk).delete()
  • 这就是我要找的。有用,但不是正确答案。
  • 警告!这是一个非常危险的功能,因为它会删除整个关系表!
【解决方案3】:

使用my_object.relations.clear()

【讨论】:

    【解决方案4】:

    要删除所有相关对象而不删除它们,只需使用:

    my_object.relations.remove(*my_object.relations.all())
    

    【讨论】:

      【解决方案5】:

      models.py

      # consider Author & Book model
      class Author(models.Model):
          name = models.CharField(max_length=200)
      
      class Book(models.Model):
          authors = models.ManyToManyField(Author, related_name='books')
          title   = models.CharField(max_length=200)
          desc    = models.TextField()
      

      在这里,我们假设一本书有很多作者,而一个作者有很多书。

      书(M)(M)作者

      1. 现在,我们找到了a book all authors 和一些操作
      books = Book.objects.all()
      if len(books) > 0:
          book = books[0]
      
          # first book all authors
          first_book_all_authors = book.authors.all()
          
          # clear/remove all authors | (not same as delete)
          book.authors.clear()
          
          # add a author
          new_author = Author.objects.create(name = "Author name: Jhon smith")
          book.authors.add(new_author)
      
          # add multiple author
          all_authors = Author.objects.all()
          book.authors.set(all_authors)
      
          # remove a author
          auth1 = Author.objects.filter(pk = 1).first()
          book.authors.remove(auth1) # if auth1 is not None, then it's remove this author
      
      1. 现在,您可以找到a author all books 和一些操作
      authors = Author.objects.all()
      if len(authors) > 0:
          auth = authors[0]
      
          # first author all book
          first_author_all_books1 = auth.books.all()   # if set related field name | related_name='books'
          first_author_all_books2 = auth.book_set.all() # if not set related field name | 'modelName'_set.all()
          
          # clear/remove all books | (not same as delete)
          auth.books.clear()
          
          # add a book
          new_book = Book.objects.create(title = "new book", desc="book desc")
          auth.books.add(new_book)
      
          # add multiple book
          all_books = Book.objects.all()
          auth.books.set(all_books)
      
          # remove a book
          book = Author.objects.filter(pk = 1).first()
          auth.books.remove(book) # if book is not None, then it's remove this book
          
      

      注意:简单思考

      【讨论】:

        猜你喜欢
        • 2023-01-27
        • 2014-01-10
        • 1970-01-01
        • 1970-01-01
        • 2016-04-12
        • 1970-01-01
        • 2012-05-06
        • 2016-05-06
        • 1970-01-01
        相关资源
        最近更新 更多