title: Django: How to efficiently add objects to ManyToManyFields of multiple objects?
body: <p>We have a student and class many to many relation, where a student can take multiple classes, and a class can be taken by multiple students.</p>
  <pre><code>class Class(models.Model):
      name = models.CharField(null=False, max_length=128)
  
  class Student(models.Model):
      first_name = models.CharField(null=False, max_length=64)
      classes = ManyToManyField(Class)
  </code></pre>
  <p>What is the fastest way to add some object or a whole <code>QueryList</code> (in our case: all classes with <em>Biology</em> in their name), to the <code>ManyToManyField</code> of each object in another <code>QueryList</code> (in our case: all students whose first name is <em>Linus</em>)?</p>
  <p>The current way I am doing it is:</p>
  <pre><code>biology_classes = Class.objects.filter(name__contains='Biology')
  linuses = Student.object.filter(first_name='Linus')
  for linus in linuses:
      linus.classes.add(*biology_classes)
  </code></pre>
  <p>Which I guess hits the db for each student. I wonder if there is maybe some way to do it all &quot;at once&quot;. Maybe something that looks something like this:</p>
  <pre><code>linuses.classes.add(*biology_classes) # (this does not work)
  </code></pre>