django-autocomplete-light simple usage

12,504

Select widget is default for ModelChoiceField

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 ?

Use autocomplete_light.ChoiceWidget instead

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/.

Ensure that you have jquery properly loaded and that autocomplete-light's staticfiles are loaded too !

Alternatives

FTR: another way is possible, using autocomplete_light.modelform_factory using shortcuts like autocomplete_light.modelform_factory or autocomplete_light.get_widgets_dict. API docs are passable but it does not beat reading the source code.

All in all, I think the easiest for you is using the get_widgets_dict shortcut if you are using a ModelForm.

Hidden docs

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.

I know that the docs have a problem, hence the docs_rewrite branch. Right now I'm focusing on improving mobile support.

Share:
12,504

Related videos on Youtube

Serafeim
Author by

Serafeim

Serafeim S. Papastefanos received his Diploma (2005) and Phd (2010) in EE from the National Technical University of Athens. He has been awarded for his academic progress from State Scholarships Foundation (SSF). His research interests include network management, network protocols, video coding, video indexing and video streaming. Since then he is working as a Software Engineer, starting from automating business processes with the Appian platform (heavy usage of Java/Javascript) and continuing with Django web development. He also keeps a small blog about various programming stuff @ http://spapas.github.io/ SOreadytohelp

Updated on September 14, 2022

Comments

  • Serafeim
    Serafeim over 1 year

    I am trying to understand how to use django-autocomplete-light for an existing project. This seems like a good autocomplete solution for django for which I am already using normal ModelChoiceFields.

    So, let's say that I have a Model named MyModel that has an id and a name. 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

    What are the required steps to have that ? I've already added 'autocomplete_light' to INSTALLED_APPS and

    url(r'autocomplete/', include('autocomplete_light.urls')),
    

    to urls.py and

    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.

  • Serafeim
    Serafeim about 11 years
    Thanks a lot this works with the addition of adding autocomplete_light.register(MyModel) !
  • jpic
    jpic about 11 years
    Just override choices_for_request.