{"id":5190,"library":"django-multiselectfield","title":"Django Multiple Select Field","description":"django-multiselectfield provides new model and form fields for Django, allowing users to select multiple options from a predefined list. The selected values are stored in the database as a CharField containing comma-separated values. The current version is 1.0.1, and the project has an irregular release cadence, with a significant 1.0.0 release in June 2025 that introduced breaking changes.","status":"active","version":"1.0.1","language":"python","source_language":"en","source_url":"https://github.com/goinnn/django-multiselectfield","tags":["django","forms","fields","multiselect","model-field"],"install":[{"cmd":"pip install django-multiselectfield","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"This is a Django app, requiring Django 3.2+ for full compatibility.","package":"Django","optional":false}],"imports":[{"note":"Used for creating model fields that allow multiple selections.","symbol":"MultiSelectField","correct":"from multiselectfield import MultiSelectField"},{"note":"Introduced in v1.0.0 for sortable multi-select fields; requires jQuery and jQuery UI.","symbol":"SortMultiSelectField","correct":"from multiselectfield import SortMultiSelectField"}],"quickstart":{"code":"from django.db import models\nfrom multiselectfield import MultiSelectField\n\n# In your settings.py, ensure 'multiselectfield' is in INSTALLED_APPS\n# INSTALLED_APPS = [\n#     # ...\n#     'multiselectfield',\n#     # ...\n# ]\n\nMY_CHOICES = (\n    ('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\nclass MyModel(models.Model):\n    my_field = MultiSelectField(choices=MY_CHOICES, default=['item_key1', 'item_key5'])\n    my_field_with_limits = MultiSelectField(\n        choices=MY_CHOICES,\n        min_choices=2,\n        max_choices=3,\n        max_length=100 # Adjust max_length based on expected comma-separated string length\n    )\n\n    def __str__(self):\n        return f\"{self.my_field} - {self.my_field_with_limits}\"\n\n# After defining your model, run:\n# python manage.py makemigrations\n# python manage.py migrate","lang":"python","description":"To use `MultiSelectField`, define your choices as a tuple of tuples. Add `multiselectfield` to your `INSTALLED_APPS` and then define a model field using `MultiSelectField`. Remember to run `makemigrations` and `migrate`."},"warnings":[{"fix":"Remove any references to `MSFList` or `MSFFlatchoices` from your code. If you were using them for `list_display`, consider using `get_FOO_display` method on your model or a custom admin method.","message":"Version 1.0.0 removed `MSFList` and `MSFFlatchoices`. These classes were intended for `admin.list_display` but never functioned correctly.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure all choices provided to `MultiSelectField` are string-based. For example, use `(('1', 'Item 1'), ('2', 'Item 2'))` instead of `((1, 'Item 1'), (2, 'Item 2'))`.","message":"As of version 1.0.0, integer choices are no longer supported. `MultiSelectField` inherits from `CharField`, and it's impossible to reliably distinguish between integer `1` and string `'1'` upon retrieval from the database.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Use a custom method on your `ModelAdmin` or the `get_FOO_display` method on your model to format the output for `list_display`. Example: `list_display = ('get_my_field_display',)` in `ModelAdmin` where `get_my_field_display` is a method on the model.","message":"Adding a `MultiSelectField` directly to `list_display` in Django admin might not render as expected (e.g., showing a raw comma-separated string).","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure jQuery and jQuery UI are loaded in your templates when using `SortMultiSelectField` outside of the default Django admin forms. In the admin, you might need to explicitly include them in your `ModelAdmin`'s `form` definition or `change_form.html`.","message":"The `SortMultiSelectField` (introduced in v1.0.0) requires jQuery and jQuery UI to function correctly in the browser. These libraries are typically included in the Django admin interface.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use `blank=True, default=''` instead of `null=True` for `MultiSelectField` if the field is optional.","message":"As a `CharField` subclass, `MultiSelectField` stores values as comma-separated strings. Avoid setting `null=True` on `CharField`s in Django, as it's generally recommended to use `blank=True` and `default=''` for string-based fields.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"search_vec":"'1.0.0':58 '1.0.1':46 '2025':62 'allow':16 'break':65 'cadenc':54 'chang':66 'charfield':36 'comma':39 'comma-separ':38 'contain':37 'current':43 'databas':33 'django':1,6,15,67 'django-multiselectfield':5 'field':4,13,69,73 'form':12,68 'introduc':64 'irregular':52 'june':61 'list':25 'model':10,72 'model-field':71 'multipl':2,20 'multiselect':70 'multiselectfield':7 'new':9 'option':21 'predefin':24 'project':49 'provid':8 'releas':53,59 'select':3,19,27 'separ':40 'signific':57 'store':30 'user':17 'valu':28,41 'version':44","created_at":"2026-04-14T01:23:04.227092+00:00","updated_at":"2026-04-16T14:32:46.913142+00:00","problems":[{"fix":"Ensure that the choices defined for `MultiSelectField` in your model use string keys, even if they represent numerical values, or explicitly convert the incoming string values to integers before validation if integer choices are strictly necessary and handled correctly throughout the application. For example, use `(('1', 'Item 1'), ('2', 'Item 2'))` instead of `((1, 'Item 1'), (2, 'Item 2'))` in your `MY_CHOICES` tuple.","cause":"This error occurs when the choices for `MultiSelectField` are defined as integers, but the values received from a form (e.g., from POST data) are strings that cannot be directly converted to integers.","error":"ValueError: invalid literal for int() with base 10: '<value>'"},{"fix":"Ensure that the choices provided to the `MultiSelectField` in your form or model match the values being submitted. If using dynamic choices, update the `choices` attribute of the field in the form's `__init__` method, or verify that the selected values are being passed as a list of valid keys.","cause":"This Django `ValidationError` occurs when the submitted values for a `MultiSelectField` do not exactly match the predefined choices, often due to dynamic choices not being correctly updated in the form's `choices` attribute or an issue with how the selected data is passed back.","error":"Select a valid choice. <value> is not one of the available choices."},{"fix":"Identify where the object becomes a list instead of a string. If you need to process individual items, iterate over the list directly. If you expect a string, ensure that the data type is indeed a string before calling `split()` (e.g., by converting it back to a string with `','.join(my_list)` if it was a list of items to be split).","cause":"This error arises when code attempts to call the `split()` method on a Python list object, but `split()` is a string method. This typically happens when data from `MultiSelectField` (which is stored as a comma-separated string) is already converted into a list, and further processing incorrectly assumes it's still a string.","error":"AttributeError: 'list' object has no attribute 'split'"},{"fix":"Update to a version of `django-multiselectfield` that officially supports Django 5.0+. If an updated version is not available, a common workaround involves commenting out the problematic `_get_flatchoices` property definition in the `multiselectfield/db/fields.py` file, as the functionality is directly provided by Django 5.0+.","cause":"This specific error indicates an incompatibility between `django-multiselectfield` and Django 5.0+ because Django's internal `CharField` (which `MultiSelectField` inherits from) no longer has the `_get_flatchoices` method; instead, `flatchoices` is directly available.","error":"AttributeError: 'super' object has no attribute '_get_flatchoices'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.0.1","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/goinnn/django-multiselectfield","docs":null,"changelog":null,"pypi":"https://pypi.org/project/django-multiselectfield/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework","database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-30","next_check":"2026-07-28","install_tag":null}}