Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is it possible to return different schemas in the same endpoint? #1471

Unanswered
luabida asked this question in Q&A
Discussion options

I'm trying to add a new Schema in the response depending on the parameter of the endpoint, but I'm getting a Pydantic error. Is it possible to include like this or I'll have to split it in two different endpoints?

api.py

@router.get(
 "/infodengue/",
 response={
 200: Union[List[HistoricoAlertaSchema], List[HistoricoAlertaPartialSchema]], # <---- Union
 404: NotFoundSchema,
 500: InternalErrorSchema,
 },
 auth=uidkey_auth,
 tags=["datastore", "infodengue"],
)
@paginate(paginator)
@csrf_exempt
def get_infodengue(
 request,
 filters: HistoricoAlertaFilterSchema = Query(...),
 full: bool = False, # <---- parameter to define the returning schema
 **kwargs,
):
 [...]
 if full:
 data = HistoricoAlerta.objects.using("infodengue").all()
 else:
 data = HistoricoAlertaPartial.objects.using("infodengue").all()
 [...]
 data = filters.filter(data)
 return data.order_by("-data_iniSE")

Traceback

pydantic_core._pydantic_core.ValidationError: 300 validation errors for NinjaResponseSchema
response.items.0
 Input should be a valid list [type=list_type, input_value=<HistoricoAlertaPartial: ...ct (411980620240520123)>, input_type=HistoricoAlertaPartial]
 For further information visit https://errors.pydantic.dev/2.11/v/list_type
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /