Last active
June 25, 2019 14:07
-
-
Save wshayes/1e04f1e6cb2b314ac4fbf689f33f66dc to your computer and use it in GitHub Desktop.
[clean_nulls function] #fastapi #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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