1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to create an update function for a Django AbstractUser

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I created signup/login with 'UserCreationForm'. How can I make update possible by using 'UserChangeForm'? models.py

    from django.contrib.auth.models
    import AbstractUser

    # Create your models here.
    class CustomUser(AbstractUser): pass
    def __str__(self): return self.username


    forms.py

    from django.contrib.auth.forms
    import UserCreationForm, UserChangeForm
    from.models
    import CustomUser

    class CustomUserCreationForm(UserCreationForm): class Meta(UserCreationForm): model = CustomUser fields = ('first_name', 'last_name', 'username', 'email')

    class CustomUserChangeForm(UserChangeForm): class Meta: model = CustomUser fields = ('first_name', 'last_name', 'username', 'email')


    views.py

    from django.shortcuts import render, redirect

    # Create your views here.
    from django.urls import reverse_lazy from django.views.generic.edit import CreateView from django.views import View

    from .forms import CustomUserCreationForm, CustomUserChangeForm from .models import CustomUser

    class SingUpView(CreateView): form_class = CustomUserCreationForm success_url = reverse_lazy('login') template_name = 'signup.html'

    #выдает ошибку
    class CustomUserUpdateView(View):
    def get(self, request, *args, **kwargs):
    user_id = kwargs.get("id")
    user = CustomUser.objects.get(id=user_id)
    form = CustomUserChangeForm(instance=user)
    return render(
    request, "users/update.html", {"form": form, "user_id": user_id}
    )

    def post(self, request, *args, **kwargs):
    user_id = kwargs.get("id")
    user = CustomUser.objects.get(id=user_id)
    form = CustomUserChangeForm(request.POST, instance=user)
    if form.is_valid():
    form.save()
    return redirect("users_list")
    return render(
    request, "users/update.html", {"form": form, "user_id": user_id}
    )


    I've been trying to create update with inheritance of the View class including 'get/post' methods, but it raises an error

    CustomUser matching query does not exist.


    I did everything google told me to activate get/post (I checked user in the settings, tried to add try/except and use get_user_model) but none of that has worked out.

    Continue reading...

Compartilhe esta Página