简体   繁体   中英

How to use “django-autocomplete-light” in inline form

I would like to use an inline model form with a 'django-autocomplete-light field'. I'm a little bit desperate also, because I don't know 'javascript' well. This is a picture of my form. At first glance, it works as desired:

在此处输入图片说明

Unfortunately, only the first field loads correctly. If I add more fields there are errors (see pictures).

在此处输入图片说明

This is my form template where I suspect the error, because the first field works correctly as desired.

<div class="container">
<form method="post" action="">
  {% csrf_token %}
  {{ form.as_p }}

  <!-- Medication Table -->
  <table class="table">
      {{ medication.management_form }}

      {% for form in medication.forms %}
          {% if forloop.first %}
              <thead>
              <tr>
                  {% for field in form.visible_fields %}
                      <th>{{ field.label|capfirst }}</th>
                  {% endfor %}
              </tr>
              </thead>
          {% endif %}
          <tr class="{% cycle "row1" "row2" %} formset_row">
              {% for field in form.visible_fields %}
                  <td>
                      {# Include the hidden fields in the form #}
                      {% if forloop.first %}
                          {% for hidden in form.hidden_fields %}
                              {{ hidden }}
                          {% endfor %}
                      {% endif %}
                      {{ field.errors.as_ul }}
                      {{ field }}
                  </td>
              {% endfor %}
          </tr>
      {% endfor %}
  </table>



  <input type="submit" value="Submit Form"/>


  <script type="text/javascript" src="{% static '/js/core/jquery.3.2.1.min.js' %}"></script>
  {{ form.media }}

    <!-- script for add, delete, update -->

    <script src="{% static 'formset/jquery.formset.js' %}"></script>
    <script type="text/javascript">
        $('.formset_row').formset({
            addText: 'add medication',
            deleteText: 'remove',
            prefix: 'medication_set'
        });
    </script>


</div>

After hours googling and going through other answers, what worked for me was this. I added a key named clone in the defaults of the jquery.formset.js with default value as true.

    /* Setup plugin defaults */
    $.fn.formset.defaults = {
        prefix: 'form',                  // The form prefix for your django formset
        formTemplate: null,              // The jQuery selection cloned to generate new form instances
        clone: true,                     // Set this value to false when using autocomplete in formset
        addText: 'add another',          // Text for the add link
        deleteText: 'remove',            // Text for the delete link
        addCssClass: 'add-row',          // CSS class applied to the add link
        deleteCssClass: 'delete-row',    // CSS class applied to the delete link
        formCssClass: 'dynamic-form',    // CSS class applied to each form in a formset
        extraClasses: [],                // Additional CSS classes, which will be applied to each form in turn
        keepFieldValues: '',             // jQuery selector for fields whose values should be kept when the form is cloned
        added: null,                     // Function called each time a new form is added
        removed: null                    // Function called each time a form is deleted
    };

Then replaced the code inside addButton.click() in jquery.formset.js from

row = options.formTemplate.clone(true).removeClass('formset-custom-template')

to

row = options.formTemplate.clone(options.clone).removeClass('formset-custom-template')

Then in the template of the formset, changed formset function from this :

$('#brand_formset_div .parentdiv .form-group').formset({
                prefix: '{{ brand_formset.prefix }}',
                deleteText: 'Clear',
                deleteCssClass: 'shop-now-delete',
                addText: 'Add new Brand',
                addCssClass: 'btn btn-success ',
            });

to this (a clone key as false is inserted along with a function added that is triggered when a new row is inserted.The function hides the extra autocomplete box.)

$('#brand_formset_div .parentdiv .form-group').formset({
                prefix: '{{ brand_formset.prefix }}',
                clone: false,
                deleteText: 'Clear',
                deleteCssClass: 'shop-now-delete',
                addText: 'Add new Brand',
                addCssClass: 'btn btn-success ',
                added: function(row) {
                    $('span .select2-selection--single:odd', row || null).css("display", "none");
                }
            });

This worked fine for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM