1

I try to make second user that contain 'staff' and 'super user' in django admin built in. The result was The cannot saved it self. Then I try to edit my user account through this django admin panel, and it cannot work to. The sistem pop up a message that i have missing some form to edit, but it didn't appear.

I don't know what i must do to solve this issue

my user model:

from django.contrib.auth.models import AbstractUser
from django.db.models import CharField
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _


class User(AbstractUser):
    # First Name and Last Name do not cover name patterns
    # around the globe.
    name = CharField(_("Name of User"), blank=True, max_length=255)

    def get_absolute_url(self):
        return reverse("users:detail", kwargs={"username": self.username})

form:

from django.contrib.auth import get_user_model, forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from snowpenguin.django.recaptcha2.fields import ReCaptchaField
from snowpenguin.django.recaptcha2.widgets import ReCaptchaWidget


User = get_user_model()


class UserChangeForm(forms.UserChangeForm):
    captcha = ReCaptchaField(widget=ReCaptchaWidget())

    class Meta(forms.UserChangeForm.Meta):
        model = User


class UserCreationForm(forms.UserCreationForm):

    error_message = forms.UserCreationForm.error_messages.update(
        {"duplicate_username": _("This username has already been taken.")}
    )

    class Meta(forms.UserCreationForm.Meta):
        model = User

    def clean_username(self):
        username = self.cleaned_data["username"]

        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username

        raise ValidationError(self.error_messages["duplicate_username"])

user admin:

from django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.auth import get_user_model

from lemlit_unisan.users.forms import UserChangeForm, UserCreationForm

User = get_user_model()


@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):

    form = UserChangeForm
    add_form = UserCreationForm
    fieldsets = (("User", {"fields": ("name",)}),) + auth_admin.UserAdmin.fieldsets
    list_display = ["username", "name", "is_superuser"]
    search_fields = ["name"]

I expect can edit and create user from django admin dashboard.

Riska Kurnianto
  • 111
  • 1
  • 7
  • First of all the user change form is not correct, import it using `from django.contrib.auth.forms import UserChangeForm` same applies on `UserCreationForm` AFAIK – Moha369 Jul 31 '19 at 02:39
  • @Moha369: `from django.contrib.auth import get_user_model, forms` `forms.UserChangeForm` `forms.UserCreationForm` that was the same way import in python. I think thats not the issue, cause When Run django it didn't show any error.. – Riska Kurnianto Jul 31 '19 at 03:22

0 Answers0