Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#10957 closed (worksforme)

Inconsisten behaviour in User admin display

Reported by: Filip Gruszczyński 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: no UI/UX: no

Description (last modified by Karen Tracey)

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?

Change History (4)

comment:1 by Filip Gruszczyński, 15 years ago

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 by Karen Tracey, 15 years ago

Description: modified (diff)

Fixed formatting.

comment:3 by Karen Tracey, 15 years ago

Resolution: worksforme
Status: newclosed

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 by dc, 15 years ago

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.

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