使用django-autocomplete-light外部管理视图

时间:2016-05-13 19:11:27

标签: django django-autocomplete-light

我已阅读http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#using-autocompletes-outside-the-admin

并尝试在一个简单的html页面中使用该小部件,但它不起作用,并且没有JavaScript或Python错误。 这是我的代码:

# 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>

我想要的只是使用他们漂亮的select2小部件列出医生名称和医生ID作为自动完成中的值。我可以在没有django-autocomplete-light的情况下做到这一点,但我明确地想要学习如何使用看起来更好的小部件来做到这一点。 有人可以指导我让它运作吗?

2 个答案:

答案 0 :(得分:2)

<强>更新

您应该覆盖'doctor'模型中的Patient属性的窗口小部件,而不是Doctor模型:

class DocForm(autocomplete.FutureModelForm):
    class Meta:
        model = Patient
        fields = ('doctor',)  # or ('__all__')
        widgets = {
            'doctor': autocomplete.ModelSelect2(url='doc-autocomplete'),
        }

此外,请确保访问reverse('doc-autocomplete')网址为登录用户提供预期结果,如autocomplete tutorial.

中所述

尝试解决类似问题,我发现我的模板中没有包含{{ form.media }}

docs

中对此进行了描述
{% block footer %}
<script type="text/javascript" src="/static/collected/admin/js/vendor/jquery/jquery.js"></script>

{{ form.media }}
{% endblock %}

答案 1 :(得分:1)

我明白了:

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()

产生:

enter image description here

如果用ModelSelect2替换ModelSelect2Multiple 然后你会得到: enter image description here