How to update manytomany field in Django?

后端 未结 3 1800
一个人的身影
一个人的身影 2020-12-05 05:11

Here\'s an example:

If I have these classes

class Author(models.Model):
    name = models.CharField(max_length=45)

class Book(models.Model):
    nam         


        
3条回答
  •  眼角桃花
    2020-12-05 05:39

    for django >=1.11 documentation:

    >>> b = Blog.objects.get(id=1)
    >>> e = Entry.objects.get(id=234)
    >>> b.entry_set.add(e) # Associates Entry e with Blog b.
    >>> new_list = [obj1, obj2, obj3]
    >>> e.related_set.set(new_list)
    

    This method accepts a clear argument to control how to perform the operation. If False (the default), the elements missing from the new set are removed using remove() and only the new ones are added. If clear=True, the clear() method is called instead and the whole set is added at once.

    and refrence: How to .update m2m field in django

提交回复
热议问题