Merge pull request #657 from tcely/patch-2

Fix CommaSepChoiceField
This commit is contained in:
meeb 2025-01-29 16:45:21 +11:00 committed by GitHub
commit 30dbb1031d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -51,24 +51,28 @@ class CustomCheckboxSelectMultiple(CheckboxSelectMultiple):
class CommaSepChoiceField(models.Field): class CommaSepChoiceField(models.Field):
"Implements comma-separated storage of lists" "Implements comma-separated storage of lists"
def __init__(self, separator=",", possible_choices=(("","")), all_choice="", all_label="All", allow_all=False, *args, **kwargs): # If 'text' isn't correct add the vendor override here.
self.separator = separator _DB_TYPES = {}
def __init__(self, *args, separator=",", possible_choices=(("","")), all_choice="", all_label="All", allow_all=False, **kwargs):
super().__init__(*args, **kwargs)
self.separator = str(separator)
self.possible_choices = possible_choices self.possible_choices = possible_choices
self.selected_choices = [] self.selected_choices = []
self.allow_all = allow_all self.allow_all = allow_all
self.all_label = all_label self.all_label = all_label
self.all_choice = all_choice self.all_choice = all_choice
super().__init__(*args, **kwargs)
def deconstruct(self): def deconstruct(self):
name, path, args, kwargs = super().deconstruct() name, path, args, kwargs = super().deconstruct()
if self.separator != ",": if ',' != self.separator:
kwargs['separator'] = self.separator kwargs['separator'] = self.separator
kwargs['possible_choices'] = self.possible_choices kwargs['possible_choices'] = self.possible_choices
return name, path, args, kwargs return name, path, args, kwargs
def db_type(self, connection): def db_type(self, connection):
return 'text' value = self._DB_TYPES.get(connection.vendor, None)
return value if value is not None else 'text'
def get_my_choices(self): def get_my_choices(self):
choiceArray = [] choiceArray = []
@ -91,21 +95,13 @@ class CommaSepChoiceField(models.Field):
'label': '', 'label': '',
'required': False} 'required': False}
defaults.update(kwargs) defaults.update(kwargs)
#del defaults.required
return super().formfield(**defaults) return super().formfield(**defaults)
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
# Only include kwarg if it's not the default
if self.separator != ",":
kwargs['separator'] = self.separator
return name, path, args, kwargs
def from_db_value(self, value, expr, conn): def from_db_value(self, value, expr, conn):
if value is None: if 0 == len(value) or value is None:
self.selected_choices = [] self.selected_choices = []
else: else:
self.selected_choices = value.split(",") self.selected_choices = value.split(self.separator)
return self return self
@ -116,7 +112,7 @@ class CommaSepChoiceField(models.Field):
return "" return ""
if self.all_choice not in value: if self.all_choice not in value:
return ",".join(value) return self.separator.join(value)
else: else:
return self.all_choice return self.all_choice