crucialfelix / django-ajax-selects

jQuery UI-powered auto-complete fields for ForeignKey, ManyToMany and text fields
Other
822 stars 249 forks source link

How add to ajax request additional GET params? #300

Closed LokkiDog closed 9 months ago

LokkiDog commented 1 year ago

image

maport commented 11 months ago

I'm not sure if this is the intended method but you can do this by overriding the URL used for the AJAX query. This can be done by supplying plugin_options with a source option:

# Generate new look URL for channel mychannelname
lookup_url = '{}?myparam={}'.format(
  reverse('ajax_lookup', kwargs={'channel': 'mychannelname'}),
  param_value
)
# Instruct a field to use it
AutoCompleteSelectField('mychannelname', plugin_options={'source': lookup_url})

and then, in your lookup:

@register('mychannelname')
class MyLookup(LookupChannel):

    def get_query(self, q, request):
        optional_param = request.GET.get('myparam', None)
        if optional_param is not None:
            # do some extra filtering
LokkiDog commented 11 months ago

Thank you for answer! I'll try this:)