### Navigation

- index
- modules
- toc
- next
- previous
- jmb.core 0.8.6 documentation »
- Django: aggiunte e standardizzazioni. »
- Misc »

# Autocompletion

Autocompletion is obtained throught autocomplete_light package.

## Dynamic Autocompletion

We attach to the change of the selection of the main autocomplete to add data to the dependent autocomplete so that it will be used when issuing the request to the server.

We also take care of cleaning up stale data of the previous select from the choices box and make sure this cascade correctly in case of several autocompletes channed toghether.

This module provides `DynamicAutocompleteModelBase` a class that inherits from `autocomplete_light.AutocompleteModelBase`.

You can define a dictionary called `filters` whose keys are queryset filter keywords and whose values are dom id. The js widget will retrieve the values of the ids and send them along with the string used in the autocompletion Foreign Key Examples:

    from jmb.core.utils.autocomplete import DynamicAutocompleteModelBase

    class MyAutocomplete(DynamicAutocompleteModelBase):
        dependency_map = {
             'organization_id' : '#id_organization',
             'project_id'  : '#id_project',
        }

M2M examples:

    from jmb.core.utils.autocomplete import DynamicAutocompleteModelBase

    class MyAutocomplete(DynamicAutocompleteModelBase):
        dependency_map = {
             'organizations' : '#id_organization',
             'projects'  : '#id_project',
        }

In TabularInline case, StackedInline and similar, you can’t set the id of field, but use this syntax:

    dependency_map = {
         'corporate_id' : '#id_car_details-x-corporate',
         'brand_id' : '#id_car_details-x-brand',
         'model_id' : '#id_car_details-x-model'
    }

In this situation the widget will call the url:

    /ticket/autocomplete/MyAutocomplete/?q=san&organization_id=1530&project_id=10

that will be handled automatically by `choices_for_request`

To have the complete list of all choice set minimum_characters to 0 in autocomplete_js_attributes dictionary, example:

    class CustomerContactAutocomplete(ContactAutocomplete):
        model = Contact
        dependency_map = {
            'organization_id' : '#id_organization',
        }
        autocomplete_js_attributes = {
            'minimum_characters': 0,
        }

To add an object in autocomplete with plus button on the right, add `add_another_url_name` value in autocomplete registry, example:

    autocomplete_light.register(RouteAutocomplete, add_another_url_name='admin:event_route_add')

The object saved, will be selected automatically in the select At the moment this feature only works with single model choice field.

class `jmb.core.utils.autocomplete.``DynamicAutocompleteModelBase`(*args, **kw)
  `choices_for_request`()

    Return all choices taking into account all dynamic filters you may need to customize this

  `get_filters`()

    Return a dictionary suitable to filter self.choices using self.filters

### Navigation

- index
- modules
- toc
- next
- previous
- jmb.core 0.8.6 documentation »
- Django: aggiunte e standardizzazioni. »
- Misc »