diff --git a/app/org_eleicoes/votepeloclima/candidature/fields.py b/app/org_eleicoes/votepeloclima/candidature/fields.py index 046623b3..ba7454ed 100644 --- a/app/org_eleicoes/votepeloclima/candidature/fields.py +++ b/app/org_eleicoes/votepeloclima/candidature/fields.py @@ -1,3 +1,4 @@ +import json import sys from django import forms @@ -293,20 +294,18 @@ def decompress(self, value): return value if value is None: return [] - return [v.strip() for v in value.split(",")] + try: + return json.loads(value) + except json.JSONDecodeError: + return [] def value_from_datadict(self, data, files, name): values = [] + for key, value in data.items(): + if key.startswith(f"{name}_") and value.strip(): + values.append(value.strip()) - # Used when save values in model like a list - if name in data: - values = data.get(name) - else: - for key, value in data.items(): - if key.startswith(f"{name}_"): - values.append(value) - - return values + return json.dumps(values) def get_context(self, name, value, attrs): context = super().get_context(name, value, attrs)