How does one update - in bulk - a many to many field in the queryset of a Django data model?
For instance, I have a data model called
which_stream = models.ManyToManyField(PhotoStream)
You can access the through model of your m2n relationship via the field's
.through attribute (see the documentation). That will allow you to bulk create the necessary through model instances:
through_model = Photo.which_stream.through # gives you access to auto-created through model # print through_model # <class 'app_label.models.Photo_which_stream'> # or sth. similar through_model.objects.bulk_create([ through_model(photo_id=pk, photostream_id=for_classmates.pk) for pk in childhood_photos.values_list('pk', flat=True) ])