[Django]-How to add multiple many to many data in django shell

7👍

Something along the following lines should work. Use the field’s add method as outlined in the documentation:

p = Player.objects.get(name=whatever)
p.league_played.add(*League.objects.filter(league__in=['league_name1', 'ln2', 'ln3']))

For a better django shell, I can recommend you django-extension‘s shell_plus which has some cool features like code completion or auto imports of all your models.

Leave a comment