Skip to main content

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

Project description

Django Awesomplete

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

PyPI Build Status Software license

Requirements

  • Python >= 3.6
  • Django >= 1.11

Installation

Install the desired version with pip:

pip install django-awesomplete

Then add awesomplete to INSTALLED_APPS in your settings file:

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

Quickstart

Let's 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 AwesompleteWidgetWrapper
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': AwesompleteWidgetWrapper(
                suggestions=get_country_suggestions
            )
        }


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

Result:

Suggestions

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

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

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

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

AwesompleteWidgetWrapper

Actually, AwesompleteWidgetWrapper is a wrapper for a widget. When the widget is not defined, it defaults to TextInput.

You can specify another widget explicitly, e.g. EmailInput:

from django import forms
from awesomplete.widgets import AwesompleteWidgetWrapper
from .models import City


class CityAdminForm(forms.ModelForm):
    class Meta:
        model = City
        fields = forms.ALL_FIELDS
        widgets = {
            'email': AwesompleteWidgetWrapper(
                widget=forms.EmailInput,
                min_chars=0,
                suggestions=(
                    'noreply@mail.com',
                    'dont_disturb@mail.com',
                    'mayor@mail.com',
                    'support@mail.com',
                ),
            )
        }

You can also pass additional parameters to AwesompleteWidgetWrapper:

  • min_chars
    Minimum characters the user has to type before the autocomplete popup shows up.
    Default: 1

  • max_items
    Maximum number of suggestions to display.
    Default: 10

  • autofirst
    Should the first element be automatically selected?
    Default: True

AwesompleteTagsWidgetWrapper

This widget is a subclass of the AwesompleteWidgetWrapper and intended to be used for entering comma-separated values.

This widget can be used with django-taggit

from django import forms
from awesomplete.widgets import AwesompleteTagsWidgetWrapper
from taggit.models import Tag
from taggit.forms import TagWidget
from .models import City


def get_tag_suggestions():
    return Tag.objects.values_list(
        'name',
        flat=True
    ).order_by('name').distinct()


class CityForm(forms.ModelForm):
    class Meta:
        model = City
        fields = forms.ALL_FIELDS
        widgets = {
            'tags': AwesompleteTagsWidgetWrapper(
                widget=TagWidget,
                suggestions=get_tag_suggestions
            )
        }

Links

License

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-awesomplete-0.6.0.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_awesomplete-0.6.0-py2.py3-none-any.whl (25.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-awesomplete-0.6.0.tar.gz.

File metadata

  • Download URL: django-awesomplete-0.6.0.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for django-awesomplete-0.6.0.tar.gz
Algorithm Hash digest
SHA256 d4df7e470593846f91e1d14f20edc2f24476782d35ff7075714a0afe08fabdb2
MD5 8eaac5ce124e0472018b25ad54c8cf50
BLAKE2b-256 333924c991b3138e4d7d70f61081d0451d5dfcc1b3106c125f74e54de5a851a6

See more details on using hashes here.

File details

Details for the file django_awesomplete-0.6.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_awesomplete-0.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fb6884deeff964041b0f2650eef383378db9aacc42a666112953925725241502
MD5 f010d64b9d2a73cc194b367791807e8e
BLAKE2b-256 db4370f3509f35529a5f495f1fbd6c3ea849b911f189a4773fbe9e3c6f022a59

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page