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

forms.SchemaField.to_python(): check if value already has a target type #45

Merged
merged 5 commits into from
Jan 30, 2024

Conversation

alexey-sveshnikov
Copy link
Contributor

Hi Savva!

Thank you for the great library! I enjoy using it a lot.

This is a small fix to a problem I encountered after upgrading to v2. I think the patch is pretty much self-explanatory, except for the case which is triggered the bug. It was in a django-constance custom fields config:

CONSTANCE_ADDITIONAL_FIELDS = {
    'my_field_type': [
        'django_pydantic_field.v2.forms.SchemaField', {
            'schema': MyModelName,
        }
    ]
}

CONSTANCE_CONFIG = {
    'MY_FIELD': ({}, '...', 'my_field_type'),
}

@surenkov
Copy link
Owner

surenkov commented Jan 23, 2024

Hi @alexey-sveshnikov, thanks for bringing this up!

I could imagine how this can be a problem with the form, but this particular case is a bit elusive for me -- I haven't had any setup with django-constance, so may I kindly ask you a bit of the detail, like stack trace for example, for historical reasons, as it may be useful for django-pydantic-field users.

Another point that I want to put into consideration, is that it's not always the case that validation schema is pydantic.BaseModel. The field can accept arbitrary annotation as long as it is acceptable by pydantic itself, and checking against isinstance(..., BaseModel) may not solve the issue entirely.
Maybe slightly reversing the condition here, and perform the parsing only if the incoming data is a string which is not a JSONString, otherwise trying to adapt the data with adapter.validate_python could cover the issue better, but that needs to be tested against several cases.

@surenkov
Copy link
Owner

@alexey-sveshnikov based on the assumptions above, I prepared this PR to your base branch.

These changes are published to PyPI under 0.3.2b1 pre-release -- I would be glad if you can try them against your configuration to test if that would work...

@surenkov surenkov force-pushed the master branch 2 times, most recently from b8c1192 to 24e37c5 Compare January 30, 2024 00:23
Try to parse non-string python objects into form field schema
@alexey-sveshnikov
Copy link
Contributor Author

Thank you.

I've added bytes to the condition as well.

stack trace for example, for historical reasons, as it may be useful for django-pydantic-field users.

I actually have no stack traces. The exception I got was ValidationError and it has been handled like normal validation error (Schema didn't match for MyModel)

@alexey-sveshnikov
Copy link
Contributor Author

0.3.2b1 works fine with django-constance!

@surenkov
Copy link
Owner

surenkov commented Jan 30, 2024

Great, thank you! I'll prepare the next patch for PyPI.

@surenkov surenkov merged commit 048d8bd into surenkov:master Jan 30, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants