Skip to content

Exclude none at the schema level for contexts

David Burke requested to merge exclude-none-schema into master

The problem we have is that we want django-ninja to allow omitting fields like DeviceContext.model_id in ingest. But on serialization we want them omitted instead of defaulting to None. At all times model_id may be omitted. But we cannot use exclude_unset or exclude_none because that's at the api level. We want the schema level.

https://github.com/pydantic/pydantic/discussions/5461

There doesn't seem to be any great solution here. Alternatives:

  • Set exclude_unset (or exclude_none) on the api itself and accept minor api compatibility breakage
  • Maybe there is a less gross way to do it, but we really do want it at the schema and not the field level
  • Have two Schema's - one for validation and one for serialization. Like writing Rust structs 🙃 This is in a way very clean and explicit but adds a huge amount of code.
Edited by David Burke

Merge request reports