[英]django-autocomplete-light simple usage
I am trying to understand how to use django-autocomplete-light for an existing project. 我试图了解如何对现有项目使用django-autocomplete-light 。 This seems like a good autocomplete solution for django for which I am already using normal ModelChoiceFields. 对于我已经在使用普通ModelChoiceFields的django来说,这似乎是一个很好的自动完成解决方案。
So, let's say that I have a Model named MyModel that has an id and a name. 因此,假设我有一个名为MyModel的模型,该模型具有一个ID和一个名称。 What I'd like to know is the simplest possible way of creating a form widget that would provide me the same functionality with 我想知道的是创建表单小部件的最简单方法,该小部件将为我提供相同的功能
mymodel = forms.ModelChoiceField( required=True, queryset=ships.models.Authority.objects.all() , )
so I'd be able to add that widget to any form I wanted in order to select instances of MyModel without using selec.t 因此我可以将该小部件添加到所需的任何表单中,以便在不使用selec.t的情况下选择MyModel实例
What are the required steps to have that ? 有哪些必要步骤? I've already added 'autocomplete_light' to INSTALLED_APPS and 我已经将'autocomplete_light'添加到INSTALLED_APPS和
url(r'autocomplete/', include('autocomplete_light.urls')),
to urls.py and 到urls.py和
import autocomplete_light
autocomplete_light.autodiscover()
before 之前
admin.autodiscover()
however I am getting confused with what to do next :( 但是我对下一步做什么感到困惑:(
Please don't point me in the documentation I've already read it thoroughly. 请不要在我已经仔细阅读过的文档中指向我。
This form field does not specify a widget, so the select widget should be used by default with: 此表单字段未指定窗口小部件,因此默认情况下 , 选择窗口小部件应使用 :
mymodel = forms.ModelChoiceField(
required=True,
queryset=ships.models.Authority.objects.all(),
)
This is why you see a select field instead of an autocomplete. 这就是为什么您看到选择字段而不是自动完成的原因。
Did you read django docs about using widgets ? 您是否阅读过有关使用小部件的django文档 ?
All you have to do is specify the widget : 您所要做的就是指定小部件 :
mymodel = forms.ModelChoiceField(
required=True,
queryset=ships.models.Authority.objects.all(),
widget=autocomplete_light.ChoiceWidget('AutocompleteName')
)
If you don't know what is the name of the autocomplete, login as staff and open http://yoursite/autocomplete/
. 如果您不知道自动完成的名称是什么,请以职员身份登录并打开http://yoursite/autocomplete/
。
Ensure that you have jquery properly loaded and that autocomplete-light's staticfiles are loaded too ! 确保正确加载了jquery,并且还加载了autocomplete-light的staticfile!
FTR: another way is possible, using autocomplete_light.modelform_factory
using shortcuts like autocomplete_light.modelform_factory
or autocomplete_light.get_widgets_dict
. FTR:另一种可能的方法是,使用autocomplete_light.modelform_factory
使用诸如autocomplete_light.modelform_factory
或autocomplete_light.get_widgets_dict
类的快捷方式 。 API docs are passable but it does not beat reading the source code . API文档是可以通过的,但它不胜于阅读源代码 。
All in all, I think the easiest for you is using the get_widgets_dict shortcut if you are using a ModelForm . 总而言之, 如果您使用的是ModelForm ,我认为最方便的方法是使用get_widgets_dict快捷方式。
You might not have found the cookbook because it is a work in progress in the docs_rewrite branch, but the second part of "High level basics" provides several examples of using the widget. 您可能尚未找到该菜谱,因为它在docs_rewrite分支中尚在进行中,但是“高级基础知识”的第二部分提供了使用该小部件的几个示例。
I know that the docs have a problem, hence the docs_rewrite branch. 我知道docs有问题,因此是docs_rewrite分支。 Right now I'm focusing on improving mobile support. 目前,我专注于改善移动支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.