[Django]-Django 1.4 Many-to-Many bulk add

10👍

Of course it’s possible! You just have to create an explicit intermediate table and
then use this model’s bulk_create method.

43👍

If you want to add queryset to bulk add or remove method of many to many relation models :

qs = Article.objects.all()
publications = Publications.objects.get(id=1)

publications.article_set.add(*qs)
publications.save()
publications.article_set.remove(*qs)
publications.save()
👤zzart

1👍

An example that adds a group and assign its permissions

group = Group.objects.create(name='user_manager')
codenames = ['delete_user', 'add_user', 'view_user', 'change_user']
permissions = Permission.objects.filter(codename__in=codenames)
group.permissions.add(*permissions)

0👍

You can just use RelatedManager.set for this:

a2 = Article(headline='NASA uses Python')
a2.save()
a2.publications.set([p1, p2, p3])

The documentation also tells us that there’s no need to call .save() at the end.

Leave a comment