我已成功将合并的自动完成灯设置到我的 django 项目中。我在表单上使用它,它按预期工作。我遵循了https://django-autocomplete-light.readthedocs.org/en/docs_rewrite/quick.html?highlight=quick上的快速入门教程
但是,我使用 autocomplete_light.Choice 小部件的表单字段的表单字段名称已更改。具体来说,在我期望的输入表单标签名称的末尾附加了一个“-autocomplete”。
这是我的表格:
class SearchForm(forms.Form):
#flight_from = forms.CharField(max_length=200, required=False, widget=forms.TextInput)
flight_from = forms.ModelChoiceField(Airport.objects.all(),
widget=autocomplete_light.ChoiceWidget('AirportAutocomplete'))
flight_to = forms.CharField(max_length=200, required=False, widget=forms.TextInput)
start_date = forms.DateField(required=False, input_formats='%Y-%m-%d')
end_date = forms.DateField(required=False, input_formats='%Y-%m-%d')
max_price = forms.FloatField(required=False, widget=forms.TextInput)
这是我的 autocomplete_light_registry.py 文件:
import autocomplete_light
from models import Airport
class AirportAutocomplete(autocomplete_light.AutocompleteModelBase):
search_fields = ['name_city_country_code']
autocomplete_light.register(Airport, AirportAutocomplete)
这是表单字段在呈现后的样子:
<label for="id_flight_from">Flight from:</label>
<span id="id_flight_from-wrapper" class="autocomplete-light-widget flight_from single" data-autocomplete-name="flight_from" data-autocomplete-placeholder="Other model name ?" data-autocomplete-choice-selector="[data-value]" data-autocomplete-url="/autocomplete/AirportAutocomplete/" data-name="flight_from" data-bootstrap="normal" data-max-values="1" data-widget-ready="1">
<span id="id_flight_from-deck" class="deck div"> </span>
<input id="id_flight_from_text" class=" autocomplete" type="text" value="" name="flight_from-autocomplete" autocomplete="off" placeholder="Other model name ?">
<select id="id_flight_from" class="value-select" multiple="multiple" name="flight_from" style="display:none"> </select>
<span class="remove div" style="display:none"> X </span>
<span class="choice-template div" style="display:none">
</span>
<div style="clear: both"></div>
如您所见,输入标签名称是“flight_from-autocomplete”,我希望它只是“flight_from”。我尝试在我的 autocomplete_light_registry.py 文件中指定 autocomplete_js_attributes 和 widget_attributes,如下所示:
autocomplete_js_attributes={'name': 'flight_from'}
和
widget_js_attributes = {'name': 'flight_from'}
但这并没有改变输入标签名称属性。如果您知道如何设置此属性或覆盖小部件渲染功能,请告诉我。