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
Example.optional_feature is considered in the disabled state the when Config. enable_optional_feature == False
When Config. enable_optional_feature == True, Example.optional_feature can be set to a string or unset (None).
I've created a schema to return instances of Example:
This schema works fine when optional_feature is enabled, but when it's disabled I want to omit it from the response, because from the perspective of the user, it doesn't exist.
You can also see that the response we send presents the information differently to how it's stored in the models. It's the extra_info field that is failing validation. However, when I just return an Example instance, ninja is able to automatically convert it successfully to an ExampleSchema in a way I've not been able to replicate.
What I've tried already
In order to get the response so I can modify it, I've been looking at these pages from the docs:
It successfully returns, but then the ValidationError gets thrown.
I've also been looking through the code, attempting to replicate whatever ninja is doing - because when I just return an Example instance, ninja handles the conversion to an ExampleSchema and there's no ValidationError.
I've been experimenting with bits of code from operation.py without success.
I also tried ninja's JSONRenderer.render, but had similar issues with identifying the correct state and variables it needs to function as expected.
Other thoughts
There may be a better way of approaching this use case of fields that aren't always present, but focussing on manually serialising the object has at least given me something specific to ask about.
It might be possible to handle this by using different schemas depending on whether the optional feature is enabled, but this isn't feasible in reality because we have many optional features leading to lots of possible combinations of optional features.
The text was updated successfully, but these errors were encountered:
I'm manually serialising a response so that I can modify the contents before returning it. Having read https://django-ninja.dev/guides/response/#serializing-outside-of-views, I'm using the
.from_orm
method for my Schema, but it doesn't appear to be calling resolvers.When I return the resulting object from my API method, a
ValidationError
is thrown because resolved fields are missing.Context
In my application we have optional features that are configured on a per-customer basis, hence we have some models similar to this:
Example.optional_feature
is considered in the disabled state the whenConfig. enable_optional_feature == False
When
Config. enable_optional_feature == True
,Example.optional_feature
can be set to a string or unset (None
).I've created a schema to return instances of
Example
:This schema works fine when
optional_feature
is enabled, but when it's disabled I want to omit it from the response, because from the perspective of the user, it doesn't exist.You can also see that the response we send presents the information differently to how it's stored in the models. It's the
extra_info
field that is failing validation. However, when I just return anExample
instance, ninja is able to automatically convert it successfully to anExampleSchema
in a way I've not been able to replicate.What I've tried already
In order to get the response so I can modify it, I've been looking at these pages from the docs:
My API endpoint looks like this:
It successfully returns, but then the ValidationError gets thrown.
I've also been looking through the code, attempting to replicate whatever ninja is doing - because when I just return an
Example
instance, ninja handles the conversion to anExampleSchema
and there's no ValidationError.I've been experimenting with bits of code from operation.py without success.
I also tried ninja's
JSONRenderer.render
, but had similar issues with identifying the correct state and variables it needs to function as expected.Other thoughts
There may be a better way of approaching this use case of fields that aren't always present, but focussing on manually serialising the object has at least given me something specific to ask about.
It might be possible to handle this by using different schemas depending on whether the optional feature is enabled, but this isn't feasible in reality because we have many optional features leading to lots of possible combinations of optional features.
The text was updated successfully, but these errors were encountered: