Search code examples
djangodjango-autocomplete-light

using django-autocomplete-light outside admin view


I've read http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#using-autocompletes-outside-the-admin

and tried to use the widget within a simple html page but it does not work and there are no JavaScript or Python errors. Here is my code:

# models.py
class Doctor(models.Model):
    name = models.CharField(max_length=200)
    address = models.CharField(max_length=200, null=True, blank=True, default='')
    def __unicode__(self):
        return self.name

class Patient(models.Model):
    name = models.CharField(max_length=200)
    sample = models.OneToOneField(Sample, null=True, blank=True)
    doctor = models.ForeignKey(Doctor, null=True, blank=True, default=None)
    def __unicode__(self):
        return self.name

# views.py
class DoctorAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated():
            return Doctor.objects.none()

        qs = Doctor.objects.all()
        if self.q:
            qs = qs.filter(name__icontains=self.q)

        return qs


class DocForm(autocomplete.FutureModelForm):
    class Meta:
        model = Doctor
        fields = ('name',)
        widgets = {
            'doctor': autocomplete.ModelSelect2(url='doc-autocomplete'),
        }

class Index(generic.UpdateView):
    model = Doctor
    form_class = DocForm
    template_name = 'index.html'
    success_url = reverse_lazy('index')

    def get_object(self):
        return Doctor.objects.first()


# index.html
<html>
<head>
<script type="text/javascript" src="/static/admin/js/jquery.js"></script>
</head>
<body>
Welcome
    <div>
        <form action="" method="post">
            {% csrf_token %}
            {{ form.as_p }}
            <input type="submit" />
        </form>
    </div>
</body>
</html>

All I want is to use their nice select2 widget to list doctor names with doctor ids as values in an autocomplete. I can do it without django-autocomplete-light, but I explicitly want to learn how to do it with their widget that looks nicer. Can someone please guide me to get it working?


Solution

  • I got it:

    class PatForm(autocomplete.FutureModelForm):
        class Meta:
            model = Patient
            fields = ('doctor',)
            widgets = {
                'doctor': autocomplete.ModelSelect2Multiple(url='doc-autocomplete'),
            }
    
    class Index(generic.UpdateView):
        model = Patient
        form_class = PatForm
        template_name = 'index.html'
        success_url = reverse_lazy('index')
    
        def get_object(self):
            return Doctor.objects.first()
    

    which produces:

    enter image description here

    if you replace ModelSelect2Multiple with ModelSelect2 then you'll get: enter image description here