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

Casting to SchemaField via output_field= #74

Closed
michallowasrzechonek-silvair opened this issue Nov 15, 2024 · 0 comments · Fixed by #78
Closed

Casting to SchemaField via output_field= #74

michallowasrzechonek-silvair opened this issue Nov 15, 2024 · 0 comments · Fixed by #78

Comments

@michallowasrzechonek-silvair
Copy link

michallowasrzechonek-silvair commented Nov 15, 2024

Hi,

First of all, thanks for this package ❤️

I think there is a small bit of functionality missing - it doesn't seem supported to access annotations generated on the database side as schema fields, like so:

class Foo(pydantic.BaseModel):
    foo: int


Model.objects.annotate(foo=JSONBuildObject(Value('bar'), Value(42)), output_field=SchemaField(Foo))))

this returns a plain JSON instead of an instance of Foo.

I think the fix would be a trivial one:

class PydanticSchemaField(...):
    def from_db_value(self, *args, **kwargs):
        value = super().from_db_value(*args, **kwargs)
        return self.to_python(value)
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.

1 participant