Django Admin Tabularinline with Auto complete field

I am trying to implement an auto-complete field in a tabular inline model. Where a user selects a site and the address cell is consequently filled with the appropriate address I defined my fields in a form as below:

self.fields['sites'].widget = forms.Select(
  attrs = {
    'id': 'id_sites',
    'onchange': 'getsiteaddress(this.value)',
    'style': 'width:200px'
  },
  choices = sites_list,
)

self.fields['site_address'].widget = forms.TextInput(
  attrs = {
    'id': 'id_siteaddress',
    'style': 'width:200px'
  }
)

Everything works fine for the first row. My issue is that when I move to the second row of the inline model and select an address the cell of the first row is the one that changes. How can I call the second row?

This is my jQuery function

function getsiteaddress(site) {
  let $ = django.jQuery;

  $.get('/Delivery/deploymentsite/'+ site, function(resp) {
    $('#id_siteaddress').val(resp.data)
  });
}

Hi @ohammad,

The id attribute of an element in an HTML document must be unique. If it’s not, you end up with unexpected results. When you add a new inline form line, each input has a unique ID. You can verify this by opening up your browser’s dev tools, and inspecting the HTML when you’ve added a few form inlines.

For your case, you should look up the input with the name=["site_address"] in the same row of the input that triggered the event rather than look for the element based on a fixed ID string.