KeyError 'email' for django-authtools UserCreationForm

I am experiencing an error for which I can’t find an origin.

I believe the error stems from GitHub - fusionbox/django-authtools: A custom User model for everybody!, and disclaimer, I have asked this same question on the project’s GitHub repository over a year ago, but nobody has answered, hopefully someone may have some insights here.

Every now and then Django complains that email is not in self.fields[User.USERNAME_FIELD], when I try to open the admin 'Add user' form, see below

enter image description here

I can see that email isn’t in self.fields but why it isn’t is not clear to me.

What absolutely confuses me is that the error is sporadic:

  1. If I experience the error in my main browser window, I don’t in a new Incognito window
  2. Restarting the app makes the error go away, for some time, but then it reappears, and the only way to solve it is to restart the app.

My UserCreationForm, a child of authtools’s UserCreationForm, looks like this

class UserCreationForm(UserCreationForm):
    """
    A UserCreationForm with optional password inputs.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["password1"].required = False
        self.fields["password2"].required = False
        # If one field gets autocompleted but not the other, our 'neither
        # password or both password' validation will be triggered.
        self.fields["password1"].widget.attrs["autocomplete"] = "off"
        self.fields["password2"].widget.attrs["autocomplete"] = "off"

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = super().clean_password2()
        if bool(password1) ^ bool(password2):
            raise forms.ValidationError("Fill out both fields")
        return password2

add_fieldsets is set as follows

add_fieldsets = (
        (
            None,
            {
                "description": (
                    "Enter the new user's name and email address and click Save."
                    " The user will be emailed a link allowing him/her to login to"
                    " the site and set his/her password."
                ),
                "fields": (
                    "first_name",
                    "last_name",
                    "email",
                    "is_pi",
                ),
            },
        ),
        (
            "Password",
            {
                "description": "Optionally, you may set the user's password here.",
                "fields": ("password1", "password2"),
                "classes": ("collapse", "collapse-closed"),
            },
        ),
    )

And the user admin

@admin.register(User)
class UserAdmin(NamedUserAdmin):
    inlines = [CostUnitInline, OIDCGroupInline]
    form = UserChangeForm
    add_form = UserCreationForm

Any ideas?

What I have noticed, in the error above, is that class common.admin.UserCreationForm references UserForm as self, should this not be UserCreationForm?

Thank you very much in advance!

Вернуться на верх