Skip to content
A django app that provides suggestions while you type into the field
Python CSS JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
awesomplete
tests
.gitignore
LICENSE.txt
README.md
manage.py
setup.py
tox.ini

README.md

Django Awesomplete

A django app that provides suggestions while you type into the field.

Requirements

  • Python 3.4+
  • Django 1.11+

Quickstart

Lets assume we are making a cities app in django and our models.py is:

from django.db import models


class City(models.Model):
    name = models.CharField(max_length=255)
    country = models.CharField(max_length=255)

    def __str__(self):
        return self.name

To use suggestions we need to override widget in admin.py:

from django import forms
from django.contrib import admin
from awesomplete.widgets import AwesompleteWidget
from .models import City


def get_country_suggestions():
    """
    Get a suggestions list from existing records.
    """
    return City.objects.values_list(
        'country', flat=True
    ).order_by('country').distinct()


class CityAdminForm(forms.ModelForm):
    class Meta:
        model = City
        fields = forms.ALL_FIELDS
        widgets = {
            'country': AwesompleteWidget(
                suggestions=get_country_suggestions
            )
        }


@admin.register(City)
class CityAdmin(admin.ModelAdmin):
    form = CityAdminForm

Result:

Installation

Install the desired version with pip:

pip install django-awesomplete

Then add awesomplete to INSTALLED_APPS in your settings file:

INSTALLED_APPS = (
    # ...
    'awesomplete',
    # ...
)

Suggestions

You can pass either an iterable of strings, 2-tuples, dicts or a callable that returns such an iterable.

# iterable of strings
AwesompleteWidget(
    suggestions=['one', 'two', 'three']
)

# iterable of 2-tuples (label, value)
AwesompleteWidget(
    suggestions=(
        ('English', 'en'),
        ('Spanish', 'es')
    )
)

# iterable of dicts
AwesompleteWidget(
    suggestions=(
        {
            'label': 'English',
            'value': 'en'        
        },
        {
            'label': 'Spanish',
            'value': 'es'        
        }
    )
)

Links

License

Copyright (c) 2018 Mihail Mishakin Released under the BSD license (see LICENSE)

You can’t perform that action at this time.