Skip to content

Instantly share code, notes, and snippets.

@wshayes
Last active June 25, 2019 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wshayes/1e04f1e6cb2b314ac4fbf689f33f66dc to your computer and use it in GitHub Desktop.
Save wshayes/1e04f1e6cb2b314ac4fbf689f33f66dc to your computer and use it in GitHub Desktop.
[clean_nulls function] #fastapi #python
import boltons.iterutils
# Note that in FastAPI you can use
# https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_skip_defaults-parameter
# if you have instantiated a Pydantic model and not altered the defaults - the response_model_skip_defaults
# will remove all defaults (including non-None or empty string - any default will be removed.
def clean_nulls(item):
"""Clean null values in responses
Use response_model_skip_defaults if you are returning a pydantic model that has
null values.
Use this if you are hand-crafting the response and want to remove nulls
"""
return boltons.iterutils.remap(item, lambda p, k, v: v is not None and v is not "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment