django-multiselectfield
=======================
.. image:: https://travis-ci.org/goinnn/django-multiselectfield.png?branch=master
:target: https://travis-ci.org/goinnn/django-multiselectfield
.. image:: https://coveralls.io/repos/goinnn/django-multiselectfield/badge.png?branch=master
:target: https://coveralls.io/r/goinnn/django-multiselectfield
.. image:: https://badge.fury.io/py/django-multiselectfield.png
:target: https://badge.fury.io/py/django-multiselectfield
A new model field and form field. With this you can get a multiple select from a choices. Stores to the database as a CharField of comma-separated values.
This egg is inspired by this `snippet <http://djangosnippets.org/snippets/1200/>`_.
Supported Python versions: 2.7, 3.4+
Supported Django versions: 1.4-2.0+
Installation
============
Install with pip
----------------
.. code-block:: bash
$ pip install django-multiselectfield
Configure your models.py
------------------------
.. code-block:: python
from multiselectfield import MultiSelectField
# ...
MY_CHOICES = (('item_key1', 'Item title 1.1'),
('item_key2', 'Item title 1.2'),
('item_key3', 'Item title 1.3'),
('item_key4', 'Item title 1.4'),
('item_key5', 'Item title 1.5'))
MY_CHOICES2 = ((1, 'Item title 2.1'),
(2, 'Item title 2.2'),
(3, 'Item title 2.3'),
(4, 'Item title 2.4'),
(5, 'Item title 2.5'))
class MyModel(models.Model):
# .....
my_field = MultiSelectField(choices=MY_CHOICES)
my_field2 = MultiSelectField(choices=MY_CHOICES2,
max_choices=3,
max_length=3)
In your settings.py
-------------------
Only you need it, if you want the translation of django-multiselectfield
.. code-block:: python
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
#.....................#
'multiselectfield',
)
Customizing templates
---------------------
It is possible to customize the HTML of this widget in your form template. To do so, you will need to loop through ``form.{field}.field.choices``. Here is an example that displays the field label underneath/after the checkbox for a ``MultiSelectField`` called ``providers``:
.. code-block:: HTML+Django
{% for value, text in form.providers.field.choices %}
<div class="ui slider checkbox">
<input id="id_providers_{{ forloop.counter0 }}" name="{{ form.providers.name }}" type="checkbox" value="{{ value }}"{% if value in checked_providers %} checked="checked"{% endif %}>
<label>{{ text }}</label>
</div>
{% endfor %}
Django REST Framework
---------------------
Django REST Framework comes with a ``MultipleChoiceField`` that works perfectly with this:
.. code-block:: python
from rest_framework import fields, serializers
from myapp.models import MY_CHOICES, MY_CHOICES2
class MyModelSerializer(serializers.HyperlinkedModelSerializer):
# ...
my_field = fields.MultipleChoiceField(choices=MY_CHOICES)
my_field2 = fields.MultipleChoiceField(choices=MY_CHOICES2)
# ...
Known Bugs and Limitations
==========================
All tests pass on Django 1.4, 1.5, and 1.8+, so if you can, use a modern version of Django. However, if you must use Django 1.6 or 1.7 there are two known issues you will need to be aware of:
1. `Named groups <https://github.com/goinnn/django-multiselectfield/pull/30#issue-52149983>`_ do not render properly in Django 1.6. The workaround is to manually render the field in your form or use a custom widget. If your workaround is suitably generic, please submit a pull request with it.
2. Only in Django 1.6 and 1.7, due to `Django bug #9619 <https://code.djangoproject.com/ticket/9619>`_, passing a MultiSelectField to ``values()`` or ``values_list()`` will return the database representation of the field (a string of comma-separated values). The workaround is to manually call ``.split(',')`` on the result.
The Django bug was introduced in Django 1.6 and is fixed in Django 1.8 and onward, so ``values()`` and ``values_list()`` return a vanilla Python list of values for Django <= 1.5 and Django >= 1.8.
See `issue #40 <https://github.com/goinnn/django-multiselectfield/issues/40>`_ for discussion about this bug.
Development
===========
You can get the last bleeding edge version of django-multiselectfield by doing a clone
of its git repository:
.. code-block:: bash
git clone https://github.com/goinnn/django-multiselectfield
Example project
===============
There is a fully configured example project in the `example directory <https://github.com/goinnn/django-multiselectfield/tree/master/example/>`_. You can run it as usual:
.. code-block:: bash
python manage.py migrate # or python manage.py syncdb --noinput
python manage.py loaddata app_data
python manage.py runserver
Changelog
=========
0.1.13 (2024-06-30)
-------------------
* Return MSFList instead of a plain list from form fields (`#118 <https://github.com/goinnn/django-multiselectfield/pull/118>`_, `#135 <https://github.com/goinnn/django-multiselectfield/pull/135>`_)
* Fix CI (`#122 <https://github.com/goinnn/django-multiselectfield/pull/122>`_, `#147 <https://github.com/goinnn/django-multiselectfield/pull/147>`_, `#148 <https://github.com/goinnn/django-multiselectfield/pull/148>`_, `#151 <https://github.com/goinnn/django-multiselectfield/pull/151>`_)
* Add ``min_choices`` to defaults when converting to form field (`#123 <https://github.com/goinnn/django-multiselectfield/pull/123>`_)
* Django 5.0 support and remove old compatibility (`#148 <https://github.com/goinnn/django-multiselectfield/pull/148>`_)
Thanks to:
* `tomasgarzon <https://github.com/tomasgarzon>`_
* `aleh-rymasheuski <https://github.com/aleh-rymasheuski>`_
* `nametkin <https://github.com/nametkin>`_
* `karolyi <https://github.com/karolyi>`_
* `olivierdalang <https://github.com/olivierdalang>`_
* `PetrDlouhy <https://github.com/PetrDlouhy>`_
0.1.12 (2020-02-20)
-------------------
* Optimize multiselectfield to_python method
* Thanks to:
* `daimon99 <https://github.com/daimon99>`_
0.1.11 (2019-12-19)
-------------------
* Added support for Django 3
* Added support for Python 3.8
* Thanks to:
* `thijsBoehme <https://github.com/thijsBoehme>`_
0.1.9 (2019-10-02)
------------------
* Added support for Django 2
* Added support for Python 3.6
* Drop support for Python (2.6, 3.3)
* Thanks to:
* `hirokinko <https://github.com/hirokinko>`_
0.1.6 (2017-05-10)
------------------
* Added support for Django 1.11
* Added support for Python 3.6
* Improved rendering in Django admin
* Improved documentation
* Thanks to:
* `atten <https://github.com/atten>`_
* `ixc <https://github.comixc>`_
* `LeilaniAnn <https://github.comLeilaniAnn>`_
0.1.5 (2017-01-02)
------------------
* Added support for Django 1.8-1.10
* Added support for named groups in choices
* Added support for min_choices argument
* Various fixes
* More tests
* Thanks to:
* `danilogbotelho <https://github.comdanilogbotelho>`_
* `dmitry-krasilnikov <https://github.comdmitry-krasilnikov>`_
* `Kamil Dębowski <https://github.comkdebowski>`_
0.1.4 (2016-02-23)
------------------
* Fixed warning about SubfieldBase
* Added support for Django 1.8+
* Added support for named groups
* We now play nice with django-dynamic-fixture
* More tests
0.1.3 (2014-10-13)
------------------
* Support to Django 1.7 (I'm sorry to the delay)
* Adding get_FIELD_list function
* Fix an error when a MultiSelectField was reandonly at the admin site
* Thanks to:
* `Hernil <https://github.com/hernil>`_
* `Vasyl Stanislavchuk <https://github.com/vasyabigi>`_
* `Litchfield <https://github.com/litchfield/>`_
* `Chris-erickson <https://github.com/chris-erickson>`_
0.1.2 (2014-04-04)
------------------
* Include the spanish translations to the pypi egg
* Improvements in the readme file
* Windows OS compatibility
* Thanks to:
* `StillNewb <https://github.com/StillNewb>`_
* `Diego Yungh <https://github.com/DiegoYungh>`_
0.1.1 (2013-12-04)
------------------
* Move the multiselectfield app to parent folder
* Details
0.1.0 (2013-11-30)
------------------
* Test/example project
* Now works if the first composant of the list of tuple is an integer
* Now max_length is not required, the Multiselect field calculate it automatically.
* The max_choices attr can be a attr in the model field
* Refactor the code
* Spanish translations
* Support to python2.6
* Thanks to:
* `Daniele Procida <https://github.com/evildmp>`_
0.0.3 (2013-09-11)
------------------
* Python 3 compatible
* Fix an error, the snippet had another error when the choices were translatables
* Improvements in the README file
0.0.2 (2012-09-28)
------------------
* Fix an error, the snippet had an error.
0.0.1 (2012-09-27)
------------------
* Initial version from the next `snippet <http://djangosnippets.org/snippets/1200/>`_
Raw data
{
"_id": null,
"home_page": "https://github.com/goinnn/django-multiselectfield",
"name": "django-multiselectfield",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "django, multiple, select, field, choices",
"author": "Pablo Martin",
"author_email": "goinnn@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/dd/c3/1a326cc669fea63f22e63f6e2b2b014534a15966506e8d7fa3c232aced42/django_multiselectfield-0.1.13.tar.gz",
"platform": null,
"description": "django-multiselectfield\n=======================\n\n.. image:: https://travis-ci.org/goinnn/django-multiselectfield.png?branch=master\n :target: https://travis-ci.org/goinnn/django-multiselectfield\n\n.. image:: https://coveralls.io/repos/goinnn/django-multiselectfield/badge.png?branch=master\n :target: https://coveralls.io/r/goinnn/django-multiselectfield\n\n.. image:: https://badge.fury.io/py/django-multiselectfield.png\n :target: https://badge.fury.io/py/django-multiselectfield\n\nA new model field and form field. With this you can get a multiple select from a choices. Stores to the database as a CharField of comma-separated values.\n\nThis egg is inspired by this `snippet <http://djangosnippets.org/snippets/1200/>`_.\n\nSupported Python versions: 2.7, 3.4+\n\nSupported Django versions: 1.4-2.0+\n\nInstallation\n============\n\n\nInstall with pip\n----------------\n\n.. code-block:: bash\n\n $ pip install django-multiselectfield\n\nConfigure your models.py\n------------------------\n\n.. code-block:: python\n\n from multiselectfield import MultiSelectField\n\n # ...\n\n MY_CHOICES = (('item_key1', 'Item title 1.1'),\n ('item_key2', 'Item title 1.2'),\n ('item_key3', 'Item title 1.3'),\n ('item_key4', 'Item title 1.4'),\n ('item_key5', 'Item title 1.5'))\n\n MY_CHOICES2 = ((1, 'Item title 2.1'),\n (2, 'Item title 2.2'),\n (3, 'Item title 2.3'),\n (4, 'Item title 2.4'),\n (5, 'Item title 2.5'))\n\n class MyModel(models.Model):\n\n # .....\n\n my_field = MultiSelectField(choices=MY_CHOICES)\n my_field2 = MultiSelectField(choices=MY_CHOICES2,\n max_choices=3,\n max_length=3)\n\n\nIn your settings.py\n-------------------\n\nOnly you need it, if you want the translation of django-multiselectfield\n\n.. code-block:: python\n\n INSTALLED_APPS = (\n 'django.contrib.auth',\n 'django.contrib.contenttypes',\n 'django.contrib.sessions',\n 'django.contrib.sites',\n 'django.contrib.admin',\n\n #.....................#\n\n 'multiselectfield',\n )\n\n\nCustomizing templates\n---------------------\n\nIt is possible to customize the HTML of this widget in your form template. To do so, you will need to loop through ``form.{field}.field.choices``. Here is an example that displays the field label underneath/after the checkbox for a ``MultiSelectField`` called ``providers``:\n\n.. code-block:: HTML+Django\n\n {% for value, text in form.providers.field.choices %}\n <div class=\"ui slider checkbox\">\n <input id=\"id_providers_{{ forloop.counter0 }}\" name=\"{{ form.providers.name }}\" type=\"checkbox\" value=\"{{ value }}\"{% if value in checked_providers %} checked=\"checked\"{% endif %}>\n <label>{{ text }}</label>\n </div>\n {% endfor %}\n\n\nDjango REST Framework\n---------------------\n\nDjango REST Framework comes with a ``MultipleChoiceField`` that works perfectly with this:\n\n.. code-block:: python\n\n from rest_framework import fields, serializers\n\n from myapp.models import MY_CHOICES, MY_CHOICES2\n\n class MyModelSerializer(serializers.HyperlinkedModelSerializer):\n # ...\n my_field = fields.MultipleChoiceField(choices=MY_CHOICES)\n my_field2 = fields.MultipleChoiceField(choices=MY_CHOICES2)\n # ...\n\n\nKnown Bugs and Limitations\n==========================\n\nAll tests pass on Django 1.4, 1.5, and 1.8+, so if you can, use a modern version of Django. However, if you must use Django 1.6 or 1.7 there are two known issues you will need to be aware of:\n\n1. `Named groups <https://github.com/goinnn/django-multiselectfield/pull/30#issue-52149983>`_ do not render properly in Django 1.6. The workaround is to manually render the field in your form or use a custom widget. If your workaround is suitably generic, please submit a pull request with it.\n\n2. Only in Django 1.6 and 1.7, due to `Django bug #9619 <https://code.djangoproject.com/ticket/9619>`_, passing a MultiSelectField to ``values()`` or ``values_list()`` will return the database representation of the field (a string of comma-separated values). The workaround is to manually call ``.split(',')`` on the result.\n\n The Django bug was introduced in Django 1.6 and is fixed in Django 1.8 and onward, so ``values()`` and ``values_list()`` return a vanilla Python list of values for Django <= 1.5 and Django >= 1.8.\n\n See `issue #40 <https://github.com/goinnn/django-multiselectfield/issues/40>`_ for discussion about this bug.\n\n\nDevelopment\n===========\n\nYou can get the last bleeding edge version of django-multiselectfield by doing a clone\nof its git repository:\n\n.. code-block:: bash\n\n git clone https://github.com/goinnn/django-multiselectfield\n\n\nExample project\n===============\n\nThere is a fully configured example project in the `example directory <https://github.com/goinnn/django-multiselectfield/tree/master/example/>`_. You can run it as usual:\n\n.. code-block:: bash\n\n python manage.py migrate # or python manage.py syncdb --noinput\n python manage.py loaddata app_data\n python manage.py runserver\n\n\nChangelog\n=========\n\n0.1.13 (2024-06-30)\n-------------------\n\n* Return MSFList instead of a plain list from form fields (`#118 <https://github.com/goinnn/django-multiselectfield/pull/118>`_, `#135 <https://github.com/goinnn/django-multiselectfield/pull/135>`_)\n* Fix CI (`#122 <https://github.com/goinnn/django-multiselectfield/pull/122>`_, `#147 <https://github.com/goinnn/django-multiselectfield/pull/147>`_, `#148 <https://github.com/goinnn/django-multiselectfield/pull/148>`_, `#151 <https://github.com/goinnn/django-multiselectfield/pull/151>`_)\n* Add ``min_choices`` to defaults when converting to form field (`#123 <https://github.com/goinnn/django-multiselectfield/pull/123>`_)\n* Django 5.0 support and remove old compatibility (`#148 <https://github.com/goinnn/django-multiselectfield/pull/148>`_)\n\nThanks to:\n\n* `tomasgarzon <https://github.com/tomasgarzon>`_\n* `aleh-rymasheuski <https://github.com/aleh-rymasheuski>`_\n* `nametkin <https://github.com/nametkin>`_\n* `karolyi <https://github.com/karolyi>`_\n* `olivierdalang <https://github.com/olivierdalang>`_\n* `PetrDlouhy <https://github.com/PetrDlouhy>`_\n\n0.1.12 (2020-02-20)\n-------------------\n\n* Optimize multiselectfield to_python method\n* Thanks to:\n * `daimon99 <https://github.com/daimon99>`_\n\n0.1.11 (2019-12-19)\n-------------------\n\n* Added support for Django 3\n* Added support for Python 3.8\n* Thanks to:\n * `thijsBoehme <https://github.com/thijsBoehme>`_\n\n0.1.9 (2019-10-02)\n------------------\n\n* Added support for Django 2\n* Added support for Python 3.6\n* Drop support for Python (2.6, 3.3)\n* Thanks to:\n * `hirokinko <https://github.com/hirokinko>`_\n\n0.1.6 (2017-05-10)\n------------------\n\n* Added support for Django 1.11\n* Added support for Python 3.6\n* Improved rendering in Django admin\n* Improved documentation\n* Thanks to:\n * `atten <https://github.com/atten>`_\n * `ixc <https://github.comixc>`_\n * `LeilaniAnn <https://github.comLeilaniAnn>`_\n\n0.1.5 (2017-01-02)\n------------------\n\n* Added support for Django 1.8-1.10\n* Added support for named groups in choices\n* Added support for min_choices argument\n* Various fixes\n* More tests\n* Thanks to:\n * `danilogbotelho <https://github.comdanilogbotelho>`_\n * `dmitry-krasilnikov <https://github.comdmitry-krasilnikov>`_\n * `Kamil D\u0119bowski <https://github.comkdebowski>`_\n\n0.1.4 (2016-02-23)\n------------------\n\n* Fixed warning about SubfieldBase\n* Added support for Django 1.8+\n* Added support for named groups\n* We now play nice with django-dynamic-fixture\n* More tests\n\n0.1.3 (2014-10-13)\n------------------\n\n* Support to Django 1.7 (I'm sorry to the delay)\n* Adding get_FIELD_list function\n* Fix an error when a MultiSelectField was reandonly at the admin site\n* Thanks to:\n * `Hernil <https://github.com/hernil>`_\n * `Vasyl Stanislavchuk <https://github.com/vasyabigi>`_\n * `Litchfield <https://github.com/litchfield/>`_\n * `Chris-erickson <https://github.com/chris-erickson>`_\n\n0.1.2 (2014-04-04)\n------------------\n\n* Include the spanish translations to the pypi egg\n* Improvements in the readme file\n* Windows OS compatibility\n* Thanks to:\n * `StillNewb <https://github.com/StillNewb>`_\n * `Diego Yungh <https://github.com/DiegoYungh>`_\n\n0.1.1 (2013-12-04)\n------------------\n* Move the multiselectfield app to parent folder\n* Details\n\n0.1.0 (2013-11-30)\n------------------\n\n* Test/example project\n* Now works if the first composant of the list of tuple is an integer\n* Now max_length is not required, the Multiselect field calculate it automatically.\n* The max_choices attr can be a attr in the model field\n* Refactor the code\n* Spanish translations\n* Support to python2.6\n* Thanks to:\n * `Daniele Procida <https://github.com/evildmp>`_\n\n0.0.3 (2013-09-11)\n------------------\n\n* Python 3 compatible\n* Fix an error, the snippet had another error when the choices were translatables\n* Improvements in the README file\n\n\n0.0.2 (2012-09-28)\n------------------\n\n* Fix an error, the snippet had an error.\n\n0.0.1 (2012-09-27)\n------------------\n\n* Initial version from the next `snippet <http://djangosnippets.org/snippets/1200/>`_\n",
"bugtrack_url": null,
"license": "LGPL 3",
"summary": "Django multiple select field",
"version": "0.1.13",
"project_urls": {
"Homepage": "https://github.com/goinnn/django-multiselectfield"
},
"split_keywords": [
"django",
" multiple",
" select",
" field",
" choices"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "be9e3ed6f072f1e806516dbc8c95e4ecae7b87af6757eb5d428857ea0a097e76",
"md5": "b6c5892ac38b9e52536aee902c5166c2",
"sha256": "f146ef568c823a409f4021b98781666ec2debabfceca9176116d749dc39cb8b3"
},
"downloads": -1,
"filename": "django_multiselectfield-0.1.13-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b6c5892ac38b9e52536aee902c5166c2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 14804,
"upload_time": "2024-07-01T05:40:37",
"upload_time_iso_8601": "2024-07-01T05:40:37.549698Z",
"url": "https://files.pythonhosted.org/packages/be/9e/3ed6f072f1e806516dbc8c95e4ecae7b87af6757eb5d428857ea0a097e76/django_multiselectfield-0.1.13-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ddc31a326cc669fea63f22e63f6e2b2b014534a15966506e8d7fa3c232aced42",
"md5": "4e7a0df637f6605f155bb3f8b6b500b7",
"sha256": "437d72632f4c0ca416951917632529c3d1d42b62bb6c3c03e3396fa50265be94"
},
"downloads": -1,
"filename": "django_multiselectfield-0.1.13.tar.gz",
"has_sig": false,
"md5_digest": "4e7a0df637f6605f155bb3f8b6b500b7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11704,
"upload_time": "2024-07-01T05:40:39",
"upload_time_iso_8601": "2024-07-01T05:40:39.456504Z",
"url": "https://files.pythonhosted.org/packages/dd/c3/1a326cc669fea63f22e63f6e2b2b014534a15966506e8d7fa3c232aced42/django_multiselectfield-0.1.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-01 05:40:39",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "goinnn",
"github_project": "django-multiselectfield",
"travis_ci": true,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "django-multiselectfield"
}