Skip to content

Commit

Permalink
Parse byte values as json as well
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-sveshnikov committed Jan 30, 2024
1 parent 92931df commit 2d890be
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions django_pydantic_field/v2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def to_python(self, value: ty.Any) -> ty.Any:
return None

try:
if not isinstance(value, str):
if not isinstance(value, (str, bytes)):
# The form data may contain python objects for some cases (e.g. using django-constance).
value = self.adapter.validate_python(value)
elif not isinstance(value, JSONString):
# Otherwise, try to parse incoming JSON accoring to the schema.
# Otherwise, try to parse incoming JSON according to the schema.
value = self.adapter.validate_json(value)
except pydantic.ValidationError as exc:
error_params = {"value": value, "title": exc.title, "detail": exc.json(), "errors": exc.errors()}
Expand Down
1 change: 1 addition & 0 deletions tests/v2/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SampleForm(Form):
"raw_data, clean_data",
[
('{"stub_str": "abc", "stub_list": ["1970-01-01"]}', {"stub_str": "abc", "stub_list": ["1970-01-01"]}),
(b'{"stub_str": "abc", "stub_list": ["1970-01-01"]}', {"stub_str": "abc", "stub_list": ["1970-01-01"]}),
({"stub_str": "abc", "stub_list": ["1970-01-01"]}, {"stub_str": "abc", "stub_list": ["1970-01-01"]}),
(InnerSchema(stub_str="abc", stub_list=[date(1970, 1, 1)]), {"stub_str": "abc", "stub_list": ["1970-01-01"]}),
],
Expand Down

0 comments on commit 2d890be

Please sign in to comment.