Opened 11 years ago

Closed 11 years ago

#21709 closed Bug (invalid)

Admin template bug if there is no username field (custom user)

Reported by: mpecov@… Owned by: nobody
Component: contrib.admin Version: 1.6
Severity: Normal Keywords: admin template
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Today is pretty normal to expect that someone will use a custom user model without the username field because he wants to have a simple login via email form.

If you exclude the username field from the user model, the base.html in the contrib.admin will crash because of this line:

 <strong>{% firstof user.get_short_name user.get_username %}</strong>.

So, my suggestion is to make a simple check
{% if user.username %}
in this line, and avoid the potential problems

Change History (5)

comment:1 by Cody Scott, 11 years ago

When you subclass AbstractBaseUser you need to specify a field to be used as the 'username'

USERNAME_FIELD = 'email'

comment:2 by anonymous, 11 years ago

yup, but try to put USERNAME_FIELD = 'id' there and see what happens

comment:3 by mpecov@…, 11 years ago

the function get_short_name is crashing the whole template with NotImplementedError

comment:4 by Keryn Knight <django@…>, 11 years ago

This is correct behaviour -- the reason you're seeing it crash is, I believe, because your custom user implementation is incomplete, see the "specifying a custom user model" documentation, which outlines the attributes (methods/properties) that need to exist for a complete implementation, one of which is get_short_name, which is set to raise a NotImplementedError if inheriting from AbstractBaseUser. There is, additionally, an API reference for the default auth implementation, which enumerates an overview of what is expected.

comment:5 by Simon Charette, 11 years ago

Resolution: invalid
Status: newclosed

As pointed out by Keryn, the documentation clearly states that you must implement this method when specifying a custom User model.

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