如何消除多种关系?

Nip*_*ips 49 django

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

class MyModel(models.Model):
    relations = models.ManyToManyField(OtherModel)
    ....
Run Code Online (Sandbox Code Playgroud)

如果我选择:

my_object.relations.remove(other_model_object)
Run Code Online (Sandbox Code Playgroud)

有用.

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

Bra*_*don 52

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

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

  • @sgauri 我觉得不是 100% 有信心,但我观察到的行为是,在调用 `remove` 时,只删除了“通过”记录,而没有删除 `other_model_object`。我认为 `remove` 的文档没有具体说明是否删除了 `other_model_object`,就像它对 `clear` 所做的那样。 (2认同)

Rac*_*ach 37

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

MyModel.relations.through.objects.all().delete()
Run Code Online (Sandbox Code Playgroud)

参考:

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()这样的事情. (31认同)

cam*_*100 12

my_object.relations.clear()


小智 5

模型.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()
Run Code Online (Sandbox Code Playgroud)

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

书 (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
Run Code Online (Sandbox Code Playgroud)
  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
    
Run Code Online (Sandbox Code Playgroud)

注意:简单思考