6

I'm trying to pass some data along to the autocomplete_light.AutocompleteModelBase so I can exclude some models from the search. I'm trying to use the Dependencies info in the docs here

but I can seem to get it.

The id of the input is id_alternate_version-autocomplete, so I'm trying:

$("#id_alternate_version-autocomplete").yourlabsWidget().autocomplete.data = {'id': 'foo'};

But the url called looks like http://127.0.0.1:8000/autocomplete/FooAutocomplete/?q=bar

I want: http://127.0.0.1:8000/autocomplete/FooAutocomplete/?q=bar&id=foo

How can I do something like that?

Rob L
  • 3,634
  • 2
  • 19
  • 38

2 Answers2

4

DAL provides a way to do this with "forwarding" of another rendered form field's value.

See http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#filtering-results-based-on-the-value-of-other-fields-in-the-form

Jorge Orpinel Pérez
  • 6,361
  • 1
  • 21
  • 38
0

This is how I did it:

$(document).ready(function() {
    $('form#recipe').on('change propertychange keyup input paste', function() {
        var ingredient_item_type    = $("form#recipe input[type='radio']:checked").val();
        var widget                  = $("form#recipe input#id_ingredients_text").parents('.autocomplete-light-widget');
        if(ingredient_item_type) {
widget.yourlabsWidget().autocomplete.data['hello'] = 'world';
        }
    });
});

Javascript acrobatics aside, the key observation is thus:

anything you put in the .data object of the autocomplete widget will automatically be made part of the GET request. HTH.

  • 1
    Cool. I'm gonna go ahead and give you answer credit for your solution. Though I solved it (a couple months ago) by simply writing my own jQuery.autocomplete event for the field in question. – Rob L Oct 10 '14 at 19:54
  • Thanks. I found your answer while solving it myself, and answered as a way of paying it forward to other users of this library. It is documented in a roundabout way here: https://django-autocomplete-light.readthedocs.org/en/docs_rewrite/cookbook.html#low-level-basics –  Oct 10 '14 at 21:24