Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raising validation error when trying to merge instances with ArrayField #171

Open
hanqyu opened this issue Oct 7, 2020 · 0 comments
Open

Comments

@hanqyu
Copy link

hanqyu commented Oct 7, 2020

The form validation raises error because the form render value of ArrayField into string not list.

ex.

['abc', 'bce'] -> "\[\'abc\',\'\bce\'\]"

I managed to solve this problem with detecting this value with regex and converting into list.
Please refer to the snippet below, just in case.

def convert_stringfied_value_to_list(data: QueryDict, exclude_keys: list):
    def string_to_list(text):
        try:
            return json.loads(text.replace("'", '"'))
        except json.decoder.JSONDecodeError:
            return text

    def is_stringfied_list(value: str):
        if not isinstance(value, str):
            return False
        regex = r'^\[((([\'\w\[\]]\,?\s?))*)\]$'
        return re.match(regex, value)

    new_values = {key: string_to_list(value)
                  for key, value in data.items()
                  if is_stringified_list(value) and key not in exclude_keys}
    data._mutable = True
    data.update(new_values)
    data._mutable = False
    return data

# adminactions/merge.py:110
def validate(request, master, other):
    ...
    if merge_form.is_valid():
        form = MForm(request.POST, instance=master)
        form.data = convert_stringified_value_to_list(form.data, exclude_keys=form.declared_fields.keys())
    ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants