Skip to main content

Django forms that support new items entered using Select2 with tags enabled

Project description

Build status Coverage status

django-select2-tags provides a form class and form fields to handle storing new Django model values created using Select2 fields with tags enabled. This is a rough project at the moment; it doesn’t handle required fields well nor does form validation restore tags to inputs. It may still help you out, and pull requests are welcome!

Tested on Python 2.7 and 3.5 with Django 1.10.

Like my work? Tip me! https://www.paypal.me/jessamynsmith

Installation

The development version can be installed with:

pip install -e git://github.com/jessamynsmith/django-select2-tags.git#egg=django-select2-tags

If you are developing locally, your version can be installed from the working directory with:

python setup.py.install

Usage

The simplest way to use django-select2-tags is to use select2_tags.forms.Select2ModelChoiceField and select2_tags.forms.Select2ModelMultipleChoiceField in the ModelForm for django.db.models.ForeignKey and django.db.models.ManyToManyField model fields, respectively. These fields extend their django equivalents and take the same arguments, along with the required value_field argument and an optional save_new keyword argument. Currently, this only works with nullable model fields, and you must pass required=False to the choice fields.

If you use the select2_tags.forms.Select2ModelForm in place of a regular ModelForm and set save_new=True on any Select2 choice fields, the new values will be saved for you.

Given the following models:

class MyRelatedModel(models.Model):
    name = models.CharField(max_length=20)


class MyModel(models.Model):
    my_fk_field = models.ForeignKey(MyRelatedModel, null=True, blank=True)
    my_m2m_field = models.ManyToManyField(MyRelatedModel)

You could create the following form to automatically save the select2 tag values:

from select2_tags import forms


class MyFkForm(forms.Select2ModelForm):
    class Meta:
        model = MyModel
        exclude = []

    my_fk_field = forms.Select2ModelChoiceField(
        'name', queryset=test_models.MyRelatedModel.objects.all(), required=False)
    my_m2m_field = forms.Select2ModelMultipleChoiceField(
        'name', queryset=test_models.MyRelatedModel.objects.all(), required=False)

You will be able to enter new values on the edit page and they will be saved to the database, so long as select2 is set up with tags enabled:

$("#id_my_fk_field").select2({
    tags: true
});

Development

Fork the project on github and git clone your fork, e.g.:

git clone https://github.com/<username>/django-select2-tags.git

Create a virtualenv and install dependencies:

mkvirtualenv django-select2-tags
pip install -r requirements/package.txt -r requirements/test.txt

Run tests with coverage (should be 100%) and check code style:

coverage run manage.py test
coverage report -m
flake8

Verify all supported Python versions:

pip install tox
tox

Install your local copy:

python setup.py install

Project details


Download files

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

Source Distribution

django-select2-tags-0.0.1.tar.gz (6.4 kB view details)

Uploaded Source

File details

Details for the file django-select2-tags-0.0.1.tar.gz.

File metadata

File hashes

Hashes for django-select2-tags-0.0.1.tar.gz
Algorithm Hash digest
SHA256 812b43aefb53c184139439f123b1c32f98463590ba41cb94f6c80393f0ddf950
MD5 d97a6e1188f7cb38ad1b5b9399278d2f
BLAKE2b-256 d8edaf56acfa1595f69d1646a1e604b228d9de5c229b9b3edd21608f7f920559

See more details on using hashes here.

Supported by

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