Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Serialization option to ignore DoesNotExist exception #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion flask_mongorest/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from mongoengine.base.proxy import DocumentProxy
from mongoengine.fields import EmbeddedDocumentField, ListField, ReferenceField, GenericReferenceField, SafeReferenceField
from mongoengine.fields import DictField
from mongoengine.errors import DoesNotExist

from cleancat import ValidationError as SchemaValidationError
from flask_mongorest import methods
Expand Down Expand Up @@ -442,7 +443,13 @@ def get(obj, field_name, field_instance=None):

# if the field is callable, execute it with `obj` as the param
if hasattr(self, field) and callable(getattr(self, field)):
value = getattr(self, field)(obj)
try:
value = getattr(self, field)(obj)
except DoesNotExist:
if kwargs.get('ignore_broken_references', False):
value = None
else:
raise

# if the field is associated with a specific resource (via the
# `related_resources` map), use that resource to serialize it
Expand Down