You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pydantic allows you to create model instances by explicitly passing values directly to the model via the __init__ method (see Basic model usage).
Generally speaking, the same can be done with ninja, except when the Schema defines resolvers, which run even though an explicit value has already been passed to the instance, and cause a ValidationError.
In my case, I want to manually create instances so that I can easily write unit tests for the schema without having to create Django model instances that are costly and complex to setup.
Seems like it's a similar issue, or even the same issue, as reported in #871
Expected Result
The SimplifiedExampleSchema instance is created without error
Actual Result
A ValidationError is raised & pytest produces output similar to this:
E pydantic_core._pydantic_core.ValidationError: 2 validation errors for SimplifiedExampleSchema
E foo
E Error extracting attribute: KeyError: 'foo' [type=get_attribute_error, input_value=<DjangoGetter: {'foo...bar': 'bar'}>, input_type=DjangoGetter]
E For further information visit https://errors.pydantic.dev/2.10/v/get_attribute_error
E bar
E Error extracting attribute: KeyError: 'bar' [type=get_attribute_error, input_value=<DjangoGetter: {'foo...bar': 'bar'}>, input_type=DjangoGetter]
E For further information visit https://errors.pydantic.dev/2.10/v/get_attribute_error
Versions:
Python version: 3.10.16
Django version: 4.2.17
Django-Ninja version: 1.3.0
Pydantic version: 2.10.3
The text was updated successfully, but these errors were encountered:
crbunney
changed the title
[BUG] Resolvers called even when models are created via __init__
[BUG] Resolvers called even when schemas are created via __init__
Jan 23, 2025
Pydantic allows you to create model instances by explicitly passing values directly to the model via the
__init__
method (see Basic model usage).Generally speaking, the same can be done with ninja, except when the Schema defines resolvers, which run even though an explicit value has already been passed to the instance, and cause a ValidationError.
In my case, I want to manually create instances so that I can easily write unit tests for the schema without having to create Django model instances that are costly and complex to setup.
Seems like it's a similar issue, or even the same issue, as reported in #871
How to reproduce
Run the test code using
pytest
:Expected Result
The
SimplifiedExampleSchema
instance is created without errorActual Result
A ValidationError is raised &
pytest
produces output similar to this:Versions:
The text was updated successfully, but these errors were encountered: