[Fixed]-Filter user in django and add to a existing group

1👍

You need the user id or the user object itself to add a user with group.user_set.add.

You can either retrieve the user object from the username, provided usernames are unique and add that:

user = User.objects.get(username=user_request)
group.user_set.add(user)

Or change your AddUser model to store the requester’s id via a OneToOne field or ForeignKey instead of username.

0👍

Django default groups doesn’t allow you to add username into it.
You have to add User objects.
Try

add_user_object = AddUser.objects.filter(owner=request.user).get()
group.user_set.add(add_user_object.owner)

Leave a comment