Differentiate between `null` and missing fields in API responses
mainIn API responses, a field that is explicitly null or missing entirely will both result in None in Python. To distinguish between them, check the .model_fields_set attribute on the response object. If the field name is not in .model_fields_set, the key was missing from the JSON; otherwise, it was explicitly set to null.
if response.my_field is None:
if 'my_field' not in response.model_fields_set:
print('Got json like {}, without a "my_field" key present at all.'.format(response))
else:
print('Got json like {"my_field": null}.')