Pass extra arguments to autocomplete

Skip to first unread message

Adam Dobrawy

Aug 23, 2016, 4:21:22 AM8/23/16
to yourlabs

I would like as about django-autocomplete-light usage. I have dynamic forms with data depends on kwargs passes to Form. I would like pass that data to widgets to pass that to Select2QuerySetView. 

For example views:
class UserMonitoringAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        qs = get_user_model().objects
        if self.q:
            qs = qs.filter(name__icontains=self.q)
        if 'monitoring' in self.request.GET:
            qs = qs.filter(monitoring=self.request.GET['monitoring'])
        return qs.all()

For example forms:
class MonitoringForm(SingleButtonMixin, UserKwargModelFormMixin, forms.ModelForm):
    def __init__(self, *args, **kwargs):
        monitoring = kwargs.get('monitoring', None)
        super(MonitoringForm, self).__init__(*args, **kwargs)
        if monitoring:
            self.fields['user'].widget = autocomplete.ModelSelect2(url='users:autocomplete',
            self.fields['user'].queryset = get_user_model().objects.filter(monitoring=monitoring)
    class Meta:
        model = Monitoring
        fields = ['name', 'description', 'notify_alert', 'template', 'user']

I see example of ```linked_data```, but that use data from pass into HTML forms and ```secure_data``` which use data from context_processor (self.request.user), so this is a bit different than data from django.forms.Form instance.


James Pic

Aug 24, 2016, 8:55:58 AM8/24/16
to yourlabs

Perhaps use a hidden field and forward it to the view ?

You received this message because you are subscribed to the Google Groups "yourlabs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit
Reply all
Reply to author
0 new messages