Created
August 1, 2018 01:26
-
-
Save rg3915/7bf1fa040f03ab2ad6f2d7b3c3f15fd9 to your computer and use it in GitHub Desktop.
bulk_create para ManyToMany no Django com through
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
users = User.objects.all() | |
simpleuser = Group.objects.get(name='simpleuser') | |
user_groups_list = [] | |
# https://stackoverflow.com/a/34093544 | |
for user in users: | |
user_groups = User.groups.through(user_id=user.pk, group_id=simpleuser.pk) | |
user_groups_list.append(user_groups) | |
User.groups.through.objects.bulk_create(user_groups_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment