[Django]-Django: Most efficient Many-To-Many-Relationship addition without through-model?

3๐Ÿ‘

โœ…

As of django 1.4, you can make use of a bulk_create method to create numerous B objects in one go, then add them to the A.b ManyToMany

So create a list of B objects, bulk create them, then add them (all at once) to the ManyToMany relationship of your A instance(s):

l = [
    B(...),
    B(...),
    B(...),
    ...
]
B.objects.bulk_create(l)
a.b.add(*o)
๐Ÿ‘คTimmy O'Mahony

Leave a comment