serializer field의 값을 가져오는 도중 ObjectDoesNotExist에러 발생하면 그냥 None을 반환한다.
e.g. code = serializers.CharField(source='notification.event_type.code')
에서 notification이 event_type 외래키로 참조하는 CommunicationEventType 인스턴스가 존재하지 않으면 .data[’code’]
는 None이다.
[rest_framework/fields.py]
def get_attribute(instance, attrs):
"""
Similar to Python's built in `getattr(instance, attr)`,
but takes a list of nested attributes, instead of a single attribute.
Also accepts either attribute lookup on objects or dictionary lookups.
"""
for attr in attrs:
try:
if isinstance(instance, Mapping):
instance = instance[attr]
else:
instance = getattr(instance, attr)
except ObjectDoesNotExist:
return None
if is_simple_callable(instance):
try:
instance = instance()
except (AttributeError, KeyError) as exc:
# If we raised an Attribute or KeyError here it'd get treated
# as an omitted field in `Field.get_attribute()`. Instead we
# raise a ValueError to ensure the exception is not masked.
raise ValueError('Exception raised in callable attribute "{}"; original exception was: {}'.format(attr, exc))
return instance