django user update form not updating

I have a detailed user view that has a button for user updates. The user update form is inside a modal, for that, I am using a FormView ModelForm and a TbUser. I don’t get how the form validation works but the fields are correct. When I update something for a user, I get an error, TbUser with username already exists, which means the code does not update the user but tries to add a new one. Also, I want to redirect to user-detail page after submit.

views.py

class UserUpdateView(LoginRequiredMixin, SuccessMessageMixin, FormView):
    form_class = UserUpdateForm
    template_name = 'users/modals/user_update_modal.html'
    success_message = "User updated successfully."

    def get_form_kwargs(self):
        kw = super().get_form_kwargs()
        kw['request'] = self.request
        return kw

    def form_valid(self, form):
        obj = form.save(commit=False)
        print(obj.username)
        print('valid')
        TbUser.objects.filter(id=self.request.user.id).update(username=obj.username, real_name=obj.real_name,
                                                              email=obj.email, cellphone=obj.cellphone,
                                                              department=obj.department, role=obj.role)

    def form_invalid(self, form):
        messages.error(self.request, form.errors)
        # Where to redirect here? I want to 

    def get_success_url(self):
        return reverse('user-detail', kwargs={'pk': self.formclass})

forms.py

class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField()

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request
        if request.user.customer:
            self.fields['department'].queryset = TbDepartment.objects.filter(
                customer=request.user.customer)
            self.fields['role'].queryset = TbRole.objects.filter(
                customer=request.user.customer)

    class Meta:
        model = TbUser
        fields = ['username', 'real_name', 'email',
                  'cellphone', 'department', 'role']

urls.py

urlpatterns = [
    path('users-list/', views.UsersListView.as_view(), name='users-list'),
    path('user-detail/<str:pk>/',
         views.UserDetailView.as_view(), name='user-detail'),
    path('tb-user-update-form/<str:pk>/update/',
         views.UserUpdateView.as_view(), name='tb-user-update-form'),
]

Answer

You need to override the get_object method to let it point to the object you want to update.

A FormView will construct a form *without looking for an object, you can use an UpdateView [Django-doc] to fetch the object with get_object, and then inject this in the form to update that object:

from django.views.generic import UpdateView

class UserUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
    form_class = UserUpdateForm
    template_name = 'users/modals/user_update_modal.html'
    success_message = "User updated successfully."

    def get_form_kwargs(self):
        kw = super().get_form_kwargs()
        kw['request'] = self.request
        return kw

    def get_object(self, *args, **kwargs):
        return self.request.user

    def form_invalid(self, form):
        messages.error(self.request, form.errors)
        return super().form_invalid(form)

    def get_success_url(self):
        return reverse('user-detail', kwargs={'pk': self.object.pk })