Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#8194 closed (fixed)

Admin form keyboard focus does not respect fields or fieldsets options

Reported by: mdhancher Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords: newforms-admin
Cc: ciantic@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

Description

When you re-order the fields in an admin add form using the fields or fieldsets options in your ModelAdmin class, the initial keyboard focus in the admin add/change form does not respect it.

The keyboard focus is set by a JavaScript call to document.getElementById("id_foo").focus(), where the ID is specified in the change_form.html template as {{adminform.first_field.auto_id}}. So an underlying issue is that AdminForm.first_field() (in contrib/admin/options.py) does not respect the re-ordering. I am experiencing this using the current Subversion HEAD, revision 8272.

For example, consider this simple model in models.py:

class MyModel(models.Model):
    foo = models.CharField(max_length=255)
    bar = models.CharField(max_length=255)

and this simple admin configuration in admin.py:

class MyModelAdmin(admin.ModelAdmin):
    fields = ('bar', 'foo')

admin.site.register(MyModel,MyModelAdmin)

Then try to add a new MyModel object using the admin interface, and notice that they keyboard focus starts out at the bottom rather than the top of the form.

Attachments (4)

8298.patch (983 bytes) - added by Fred Bartle <fredbartle@…> 6 years ago.
sets focus to first field in admin
8298.2.patch (982 bytes) - added by Fred Bartle <fredbartle@…> 6 years ago.
8194_against_8630.diff (982 bytes) - added by fredbartle 6 years ago.
8194_against_8752.diff (441 bytes) - added by fredbartle 6 years ago.
Allows access to BoundField

Download all attachments as: .zip

Change History (13)

comment:1 Changed 6 years ago by brosner

  • milestone set to 1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Changed 6 years ago by Fred Bartle <fredbartle@…>

sets focus to first field in admin

comment:2 Changed 6 years ago by Fred Bartle <fredbartle@…>

Added patch that respects fields or fieldsets specified in ModelAdmin. Required access to forms.BoundField.

Changed 6 years ago by Fred Bartle <fredbartle@…>

comment:3 Changed 6 years ago by Fred Bartle <fredbartle@…>

Fixed a typo in the first patch.

comment:4 Changed 6 years ago by Ciantic

  • Cc ciantic@… added
  • Keywords newforms-admin added

comment:5 Changed 6 years ago by fredbartle

  • Has patch set

Suppose I should check this box!

Changed 6 years ago by fredbartle

comment:6 Changed 6 years ago by jacob

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

(In [8681]) Fixed #8194: AdminForm.first_field now correctly returns the first field when using custom field orders.

comment:7 Changed 6 years ago by fredbartle

  • Patch needs improvement set
  • Resolution fixed deleted
  • Status changed from closed to reopened

I'm reopening because forms.BoundField is not accessible for this patch to work. As of r8681, if admin fields or fieldsets are specified, the following html is created from the template:

<script type="text/javascript">document.getElementById("").focus();</script>

Using a try/except block around L25 in django/contrib/admin/helpers.py shows an AttributeError being raised.

'module' object has no attribute 'BoundField'

Just a side question here, I'm listed under AUTHORS as 'hambaloney' for another ticket I was involved with, is it possible to change that to my name?

Changed 6 years ago by fredbartle

Allows access to BoundField

comment:8 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [8774]) Fixed #8194 (again): correctly focus on the first declared field in the admin. Thanks to fredbartle for catching my silly mistake the first time 'round.

comment:9 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

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.