{ "info": { "author": "Kelvin Wong", "author_email": "code@kelvinwong.ca", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.2", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Topic :: Internet :: WWW/HTTP" ], "description": "****************************\ndjango-select-multiple-field\n****************************\n\nSelect multiple choices in a single Django model field. Use whenever you want\nto store multiple choices in a field without using a many-to-many relation.\n\n.. figure:: https://github.com/kelvinwong-ca/django-select-multiple-field/raw/master/docs/images/select_multiple_cropped.jpg\n\n Rendered using the multiselect.js plugin for jQuery [#]_\n\n.. [#] jquery.multi-select.js https://github.com/lou/multi-select\n\nQuick Start\n===========\n\n.. important::\n\n Field attribute ``max_length`` must be set to a value longer than your\n longest expected encoded string\n\n.. important::\n\n Choices must be strings, not integers.\n\nIn your models add the select field choices normally::\n\n # models.py\n\n class Pizza(models.Model):\n ANCHOVIES = 'a'\n BLACK_OLIVES = 'b'\n PEPPERONI = 'p'\n MOZZARELLA = 'm'\n TOPPING_CHOICES = (\n (ANCHOVIES, 'Anchovies'),\n (BLACK_OLIVES, 'Black olives'),\n (PEPPERONI, 'Pepperoni'),\n (MOZZARELLA, 'Mozzarella'),\n )\n\n toppings = SelectMultipleField(\n max_length=10,\n choices=TOPPING_CHOICES\n )\n\nUse a generic view or a modelform as usual. In your template you can use a regular form tag::\n\n # template_form.html\n\n
\n\nThis renders the following HTML::\n\n # create.html\n\n \n\nDisplaying stored choices\n-------------------------\n\nTo display your choices, you will need to decode the field contents. This can\nbe accomplished with a template tag::\n\n # templatetags/pizza_tags.py\n\n def decode_pie(ingredients):\n \"\"\"\n Decode pizza pie toppings\n \"\"\"\n decoder = dict(Pizza.TOPPING_CHOICES)\n decoded = [decoder[t] for t in ingredients]\n decoded.sort()\n return ', '.join(decoded)\n\n register.filter('decode_pie', decode_pie)\n\nIn your template you need to import your tags and use them::\n\n # details.html\n\n {% load pizza_tags %}\n\n {{ pizza.toppings|decode_pie }}\n\nEncoding the choices\n====================\n\nThe choices that are selected are stored as comma-delimited text. Consider a\npizza with the following toppings.\n\n * Pepperoni\n * Mozzarella\n\nThis would be stored as a character field as::\n\n p,m\n\nThis encoded string is decoded to a Python list using functions in the codecs\nmodule::\n\n >>> from select_multiple_field.codecs import *\n >>> encoded = 'a,b,c'\n >>> decoded = decode_csv_to_list(encoded)\n >>> print decoded\n [u'a', u'b', u'c']\n >>> print type(decoded)\n