Pass extra arguments to autocomplete

249 views
Skip to first unread message

Adam Dobrawy

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

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', XXXXXXX=monitoring.pk)
            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.

Greetings,

James Pic

unread,
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 yourlabs+unsubscribe@googlegroups.com.
To post to this group, send email to your...@googlegroups.com.
Visit this group at https://groups.google.com/group/yourlabs.
To view this discussion on the web visit https://groups.google.com/d/msgid/yourlabs/9c948d24-a718-4b0d-85ac-46bfc9a95eff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages