Opened 4 years ago

Closed 4 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


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 Changed 4 years ago by Cody Scott

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


comment:2 Changed 4 years ago by anonymous

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

comment:3 Changed 4 years ago by mpecov@…

the function get_short_name is crashing the whole template with NotImplementedError

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

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 Changed 4 years ago by Simon Charette

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