Opened 4 years ago

Closed 4 years ago

#29807 closed Bug (invalid)

Custom user model with SlugField username causes `allow_unicode` error in admin

Reported by: Jesse Owned by: Adiyat Mubarak
Component: contrib.auth Version: 2.1
Severity: Normal Keywords: slugfield allow_unicode error admin
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

After modifying the user model to use a SlugField for the username, I can no longer add or modify users in the Django admin panel because of the following error:

TypeError at /admin/user/user/6/change/
__init__() got an unexpected keyword argument 'allow_unicode'

Exception location: ...venv/lib/python3.6/site-packages/django/forms/fields.py in __init__, line 213

Inspecting the SlugField definition you can see that it accepts a param called allow_unicode:

class SlugField(CharField):
    default_validators = [validators.validate_slug]
    description = _("Slug (up to %(max_length)s)")

    def __init__(self, *args, max_length=50, db_index=True, allow_unicode=False, **kwargs):
        self.allow_unicode = allow_unicode
        if self.allow_unicode:
            self.default_validators = [validators.validate_unicode_slug]
        super().__init__(*args, max_length=max_length, db_index=db_index, **kwargs)

My user model is based off the Django auth model:

class User(AbstractUser):
  username = models.SlugField("username", max_length=20, unique=True,
    error_messages={
      "unique": "A user with that username already exists."
    })

  email = models.EmailField("email", unique=True,
    error_messages={
      "unique": "A user with that email already exists."
    })

  # Users are required to login using their email
  USERNAME_FIELD = "email"
  REQUIRED_FIELDS = [ "username" ]

The model is registered in the admin panel as follows:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from .models import User


admin.site.register(User, UserAdmin)

The error goes away if I comment out the username field and run a migration to revert back to the old username which uses a CharField.

requirements.txt:

argon2-cffi==18.3.0
cffi==1.11.5
Django==2.1.1
djangorestframework==3.8.2
pycparser==2.19
pytz==2018.5
six==1.11.0

Change History (5)

comment:1 Changed 4 years ago by Jesse

Severity: NormalRelease blocker

comment:2 Changed 4 years ago by Adiyat Mubarak

Owner: changed from nobody to Adiyat Mubarak
Status: newassigned

Hi,
Let me reproduce this on my machine and I will do a bug fix to this ticket.

comment:3 Changed 4 years ago by Adiyat Mubarak

I also had this issue when reproducing this using python 3.7 and the same requirements in above.
Will trying to fix this after this ticket accepted as an issue.

comment:4 Changed 4 years ago by Carlton Gibson

Severity: Release blockerNormal

The allow_unicode parameter was added in 2015 (f8cc464452f495fce2a3d6f7494396c8f798a1e6). A bug this may be but it's not in a new feature, and it's not likely this ever worked, so it's not a regression. Therefore it's not a release blocker.

comment:5 Changed 4 years ago by Tim Graham

Component: contrib.admincontrib.auth
Resolution: invalid
Status: assignedclosed
Summary: SlugField causes `allow_unicode` error in Django admin panelCustom user model with SlugField username causes `allow_unicode` error in admin

The cause of the issue is documented, "the following forms are tied to User and need to be rewritten or extended to work with a custom user model: UserCreationForm."

Allowing UserCreationForm to work with custom user models is tracked in #28608.

Note: See TracTickets for help on using tickets.
Back to Top