djangautocompletionlight简单外键完成显示不可编辑的下拉窗口widg

2021-02-25 04:08:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个国家模式:

    class Country(models.Model):
        name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

在UserDetails模型中由外键引用:

^{pr2}$

通过使用django autocomplete light,当插入国家时,我有一个文本小部件,它在写第一个字母时完成。在

因此,我准备了:

    class CountryAutocomplete(autocomplete.Select2QuerySetView):
        def get_queryset(self):

            qs = Country.objects.all()

        if self.q:
            qs = qs.filter(name__istartswith=self.q)

        return qs

与网址.py公司名称:

    app_name='shared'
    urlpatterns = [
        url(
            r'^country-autocomplete/$',
            CountryAutocomplete.as_view(),
            name='country-autocomplete',
        ),
    ]

对于UserDetails表单:

    class UserDetailsForm(forms.ModelForm):

        country = forms.ModelChoiceField(
            queryset=Country.objects.all(),
            widget=autocomplete.ModelSelect2(url='shared:country-autocomplete')
            )

        class Meta:
            model = UserDetails
            fields = [...,"country"]

作为我使用的模板:

{% load static %}


{% block content %}
<form method="post" action=".">
    {% csrf_token %}
    {{ user_form.as_p }}
    {{ user_details_form.as_p }}
    <input type="submit" value="Submit" />
</form>
{% endblock %}

{% block footer %}
    <script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
    <link rel="stylesheet" type="text/css" href="{% static 'autocomplete_light/select2.css' %}" />
    <script type="text/javascript" src="{% static 'autocomplete_light/jquery.init.js' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/autocomplete.init.js' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/select2.js' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/vendor/select2/dist/css/select2.css' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/vendor/select2/dist/js/select2.full.js' %}"></script>
    <script type="text/javascript" src="{% static 'autocomplete_light/forward.js' %}"></script>
    <script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
    {{ form.media }}
{% endblock %}

我已经复制了项目根目录下名为“static”的文件夹中的所有静态文件,并在“站点设置”中设置了static“URL='/static/”。 在执行get调用时,runserver日志记录为静态文件提供:

[23/Oct/2016 17:44:59] "GET /accounts/register/ HTTP/1.1" 200 2344
[23/Oct/2016 17:44:59] "GET /static/admin/js/vendor/jquery/jquery.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/autocomplete.init.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/select2.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/select2.css HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/jquery.init.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/vendor/select2/dist/css/select2.css HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/vendor/select2/dist/js/select2.full.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/autocomplete_light/forward.js HTTP/1.1" 304 0
[23/Oct/2016 17:44:59] "GET /static/admin/js/vendor/jquery/jquery.js HTTP/1.1" 304 0

当我将get直接发送到autocomplete视图时,我得到的结果是:

http://127.0.0.1:8000/shared/country-autocomplete/?q=u

{"pagination": {"more": false}, "results": [{"text": "USA", "id": 3}]}

但是,如果我转到registration form,对于country字段,我会得到这样一个小部件:

country_widget

为空,无法编辑。 我怎样才能有一个文本小部件,在键入字母时建议国家名称?在