Skip to content

Instantly share code, notes, and snippets.

@monkut
Created August 14, 2023 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkut/906bdbb62c184d1d46afeff1757af5f1 to your computer and use it in GitHub Desktop.
Save monkut/906bdbb62c184d1d46afeff1757af5f1 to your computer and use it in GitHub Desktop.
Django admin widget to allow proper multiple selection of selected checkbox entries for CheckboxSelectMultiple
class ArrayFieldCheckboxSelectMultiple(CheckboxSelectMultiple):
"""A CheckboxSelectMultiple widget for use with ArrayField which fixes the Multiple items not selected issue"""
def format_value(self, value):
"""Return selected values as a list."""
if value is None and self.allow_multiple_selected:
return []
elif self.allow_multiple_selected:
value = [v for v in value.split(",")]
if not isinstance(value, (tuple, list)):
value = [value]
results = [str(v) if v is not None else "" for v in value]
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment