Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#10957 closed (worksforme)

Inconsisten behaviour in User admin display

Reported by: gruszczy Owned by: nobody
Component: Contrib apps Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by kmtracey)

I needed to change admin site for User class from auth, because I need to make first name and last name required. I subclassed Admin class for User and changed form:

class UserChangeForm(ModelForm):
        username = RegexField(label=_("Username"), max_length=30, regex=r'^\w+$',
                help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."),
                error_message = _("This value must contain only letters, numbers and underscores."))
        first_name = CharField(label=_('first name'), max_length=30, required=True)
        last_name = CharField(label=_('last name'), max_length=30, required=True)
        
        class Meta:
                model = User

Without first_name and last_name fields defined, admin displays it translated into polish, with first letter capitalized, like this:

Imie
Nazwiski

With first_name and last_name fields defined, which have labels defined exactly as User model ('first name' and 'last name'), admin displays it translated, but not capitalized, like this:

imie
nazwisko

The other labels are capitalized and I would like to be consistent. Is there any way to make it consistent?

Attachments (0)

Change History (4)

comment:1 Changed 5 years ago by gruszczy

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I can't change the example formatting, so I repost it here:

class UserChangeForm(ModelForm):
	username = RegexField(label=_("Username"), max_length=30, regex=r'^\w+$',
		help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."),
		error_message = _("This value must contain only letters, numbers and underscores."))
	first_name = CharField(label=_('first name'), max_length=30, required=True)
	last_name = CharField(label=_('last name'), max_length=30, required=True)
	
	class Meta:
		model = User

comment:2 Changed 5 years ago by kmtracey

  • Description modified (diff)

Fixed formatting.

comment:3 Changed 5 years ago by kmtracey

  • Resolution set to worksforme
  • Status changed from new to closed

I can't explain why the label in your case isn't being capitalized, but the way you have done this is not how I would approach the problem. Rather, to avoid repeating field definitions, etc., I would create a custom user change form based off of the existing one, and simply override the required attribute for the fields in question. This works for me:

from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm

class CustomUserChangeForm(UserChangeForm):
    def __init__(self, *args, **kwargs):
        super(CustomUserChangeForm, self).__init__(*args, **kwargs)
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True

class CustomUserAdmin(UserAdmin):
    form = CustomUserChangeForm

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

When the language is set to Polish, I then get the labels for first and last name both capitalized and in bold, indicating they are required. If I try to save a user without filling in one of those fields it gets annotated with an error message "To pole jest wymagane." which I assume means "This field is required."

(Note it is particularly important to base a custom admin object for User off of the existing django.contrib.auth.admin.UserAdmin class, as it is set up to support changing the password. If you base your custom User admin object off of admin.ModelAdmin you will run into trouble when you try changing the password. Not sure if you did that or not as you didn't include the admin config portion of what you had done.)

Finally, if your ticket contains a question like "Is there any way to make it consistent?", that's a signal it is probably better suited to django-users than the ticket tracker. In the future please try django-users first, and open a ticket only if you are absolutely sure the problem is a bug in Django.

comment:4 Changed 5 years ago by dc

Django will automatically capitalize labels generated from the model fields but will not change user-provided labels for the form fields. This is a normal behaviour.

Just use lowercased names for the model fields and capitalized labels for the form fields.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.