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

Thanks Kidwind for the report and Julien Phalip for the initial
patch.
This commit is contained in:
Claude Paroz 2013-08-31 21:04:53 +02:00
parent 92a710cd03
commit 1c0c879be3
2 changed files with 8 additions and 16 deletions

View File

@ -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:

View File

@ -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 #}