Django dependent drop down using inline formset

Asked

Viewed 33 times

1

I’m following this tutorial.

How to Implement Dependent/Chained Dropdown List with Django

But I’m trying to use the inline formset. Has anyone ever used it this way? For my problem is in the following:

<script>
    $("#id_country").change(function () {
      var url = $("#personForm").attr("data-cities-url");  // get the url of the `load_cities` view
      var countryId = $(this).val();  // get the selected country ID from the HTML input

      $.ajax({                       // initialize an AJAX request
        url: url,                    // set the url of the request (= localhost:8000/hr/ajax/load-cities/)
        data: {
          'country': countryId       // add the country id to the GET parameters
        },
        success: function (data) {   // `data` is the return of the `load_cities` view function
          $("#id_city").html(data);  // replace the contents of the city input with the data that came from the server
        }
      });

    });
  </script>

At the end of the code $("#id_city").html(data); it replaces field values id_city by the value that came from the ok server...

The problem is when I use the inline formset, he puts the id's as follows:

id_city-0-exemplo... and when you add a new form he gets id_city-1-exemplo... and so on.

The problem is that in javascript I have to go through exactly the id of the field that will receive the value returned from the server.

From now on I appreciate any help.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.