Commit 1c0c879b authored by Claude Paroz's avatar Claude Paroz
Browse files

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

Thanks Kidwind for the report and Julien Phalip for the initial
patch.
parent 92a710cd
Loading
Loading
Loading
Loading
+0 −14
Original line number Diff line number Diff line
@@ -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:
+8 −2
Original line number Diff line number Diff line
@@ -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 #}