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

HttpUrl or any other pydantic specific field is not serializable #56

Closed
sshishov opened this issue Mar 26, 2024 · 1 comment · Fixed by #57
Closed

HttpUrl or any other pydantic specific field is not serializable #56

sshishov opened this issue Mar 26, 2024 · 1 comment · Fixed by #57

Comments

@sshishov
Copy link

If we have a pydantic model with any Pydantic specific fields, then the model will not be properly serialized and the crash will appear.

For instance:

import pydantic
import django_pydantic_field

class MyModel(pydantic.BaseModel):
     my_url: pydantic.HttpUrl = pydantic.HttpUrl('https://example.com/')

obj = MyModel()
field = django_pydantic_field.SchemaField(schema=MyModel)
field.get_prep_value(obj)

And the output will be:

TypeError: Object of type Url is not JSON serializable

It can be fixed if we use the mode=json when we perform _prepare_raw_value inside get_prep_value like this:

def get_prep_value(self, value: t.Any):
        value = self._prepare_raw_value(value, mode='json')
        return super().get_prep_value(value)

Or am I missing something?

@surenkov
Copy link
Owner

Yep, you're absolutely correct, thank you for reporting this!

I've prepared the fix in #57, will release it under the next patch.

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 a pull request may close this issue.

2 participants