summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2013-08-31 21:04:53 +0200
committerClaude Paroz <claude@2xlibre.net>2013-08-31 21:19:22 +0200
commit1c0c879be3a21affec3d5eb2d6b6e4f0a0803aec (patch)
treec177574a6611667cca8803f35439189873eba651
parent92a710cd03fc2248d2ca048ddf3c0334d0089068 (diff)
Fixed #17074 -- Improved first field detection in admin add form
Thanks Kidwind for the report and Julien Phalip for the initial patch.
-rw-r--r--django/contrib/admin/helpers.py14
-rw-r--r--django/contrib/admin/templates/admin/change_form.html10
2 files changed, 8 insertions, 16 deletions
diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py
index af1df5e668..a91988bc47 100644
--- a/django/contrib/admin/helpers.py
+++ b/django/contrib/admin/helpers.py
@@ -46,20 +46,6 @@ class AdminForm(object):
**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:
diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html
index e35c7328f4..b2df1dcd3d 100644
--- a/django/contrib/admin/templates/admin/change_form.html
+++ b/django/contrib/admin/templates/admin/change_form.html
@@ -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 #}