#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 Changed 18 months ago by Siecje

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

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

USERNAME_FIELD = 'email'

comment:2 Changed 18 months ago by anonymous

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

comment:3 Changed 18 months ago by mpecov@…

the function get_short_name is crashing the whole template with NotImplementedError

comment:4 Changed 18 months 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 18 months ago by charettes

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

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