Fixes #1101: Fix AJAX scripting for device component selection forms

This commit is contained in:
Jeremy Stretch 2017-04-26 12:53:14 -04:00
parent 861c8b29c0
commit 1fa084b6be
2 changed files with 8 additions and 7 deletions

View File

@ -316,7 +316,7 @@ class IPAddressForm(BootstrapMixin, ReturnURLForm, CustomFieldForm):
interface_rack = forms.ModelChoiceField(
queryset=Rack.objects.all(), required=False, label='Rack', widget=APISelect(
api_url='/api/dcim/racks/?site_id={{interface_site}}', display_field='display_name',
attrs={'filter-for': 'interface_device'}
attrs={'filter-for': 'interface_device', 'nullable': 'true'}
)
)
interface_device = forms.ModelChoiceField(

View File

@ -88,20 +88,21 @@ $(document).ready(function() {
// Determine the filter fields needed to make an API call
var filter_regex = /\{\{([a-z_]+)\}\}/g;
var match;
var rendered_url = api_url;
while (match = filter_regex.exec(api_url)) {
var filter_field = $('#id_' + match[1]);
if (filter_field.val()) {
api_url = api_url.replace(match[0], filter_field.val());
} else if ($(this).attr('nullable') == 'true') {
api_url = api_url.replace(match[0], '0');
rendered_url = rendered_url.replace(match[0], filter_field.val());
} else if (filter_field.attr('nullable') == 'true') {
rendered_url = rendered_url.replace(match[0], '0');
}
}
// If all URL variables have been replaced, make the API call
if (api_url.search('{{') < 0) {
console.log(child_name + ": Fetching " + api_url);
if (rendered_url.search('{{') < 0) {
console.log(child_name + ": Fetching " + rendered_url);
$.ajax({
url: api_url,
url: rendered_url,
dataType: 'json',
success: function (response, status) {
$.each(response, function (index, choice) {