Permalink
Browse files

Fixed #17074 -- Improved first field detection in admin add form

Thanks Kidwind for the report and Julien Phalip for the initial
patch.
  • Loading branch information...
1 parent 92a710c commit 1c0c879be3a21affec3d5eb2d6b6e4f0a0803aec @claudep claudep committed Aug 31, 2013
Showing with 8 additions and 16 deletions.
  1. +0 −14 django/contrib/admin/helpers.py
  2. +8 −2 django/contrib/admin/templates/admin/change_form.html
@@ -46,20 +46,6 @@ def __iter__(self):
**options
)
- def first_field(self):
- try:
- fieldset_name, fieldset_options = self.fieldsets[0]
- field_name = fieldset_options['fields'][0]
- if not isinstance(field_name, six.string_types):
- field_name = field_name[0]
- return self.form[field_name]
- except (KeyError, IndexError):
- pass
- try:
- return next(iter(self.form))
- except StopIteration:
- return None
-
def _media(self):
media = self.form.media
for fs in self:
@@ -66,8 +66,14 @@
{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
-{% if adminform.first_field and add %}
- <script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script>
+{% if adminform and add %}
+ <script type="text/javascript">
+ (function($) {
+ $(document).ready(function() {
+ $('form#{{ opts.module_name }}_form :input:visible:enabled:first').focus()
+ });
+ })(django.jQuery);
+ </script>
{% endif %}
{# JavaScript for prepopulated fields #}

3 comments on commit 1c0c879

@timgraham
Member

There are two tests for the old behavior which are failing:

admin_views.tests.AdminJavaScriptTest.testMultiWidgetFirsFieldFocus
admin_views.tests.AdminJavaScriptTest.testSingleWidgetFirsFieldFocus

I guess they could be updated for the new jQuery selector.

@ramiro
Member
ramiro commented on 1c0c879 Sep 1, 2013

Also, these tests have a typo ("Firs") in their names

I suspect that this could the reason they weren't found when fixing this ticket.

And also suspect I'm the one that introduced these tests :-(

@ramiro
Member
ramiro commented on 1c0c879 Sep 1, 2013

Fixed in 41feab3. Converted these tests to use Selenium as actually there is no point in testing presence of JS code that now is hard-coded in the template.

Please sign in to comment.