1👍
✅
You cannot add a member to group that does not yet exist, so you need to add the user after the group has been created for example :
if form.is_valid():
team = form.save(commit=False)
team.created_by = request.user
team.members.add(request.user)
team.save()
is now :
if form.is_valid():
team = form.save(commit=False)
team.created_by = request.user
team.save()
team.members.add(request.user) # you can only add member after the group has been created
Same in the second snippet :
team = Team.objects.create(title=title, created_by=request.user)
team.save()
team.members.add(request.user)
Source:stackexchange.com