diff options
| author | Mark Aaron Shirley | 2013-01-04 21:11:03 +0100 |
|---|---|---|
| committer | Mark Aaron Shirley | 2013-01-04 21:11:03 +0100 |
| commit | 213981cef394c6f7603c24b9a51096ffb56f6024 (patch) | |
| tree | 356de336d49fc2552ae819009a4f0de572e8525d /rest_framework/serializers.py | |
| parent | ad671022e1a43f91e0285f53bab64b7e33395eb3 (diff) | |
| download | django-rest-framework-213981cef394c6f7603c24b9a51096ffb56f6024.tar.bz2 | |
Handle ObjectDoesNotExist exceptions when serializing null reverse one-to-one
Diffstat (limited to 'rest_framework/serializers.py')
| -rw-r--r-- | rest_framework/serializers.py | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index bd54db4c..3391a262 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -293,15 +293,18 @@ class BaseSerializer(Field): Override default so that we can apply ModelSerializer as a nested field to relationships. """ - if self.source: - for component in self.source.split('.'): - obj = getattr(obj, component) + try: + if self.source: + for component in self.source.split('.'): + obj = getattr(obj, component) + if is_simple_callable(obj): + obj = obj() + else: + obj = getattr(obj, field_name) if is_simple_callable(obj): obj = obj() - else: - obj = getattr(obj, field_name) - if is_simple_callable(obj): - obj = value() + except ObjectDoesNotExist: + return None # If the object has an "all" method, assume it's a relationship if is_simple_callable(getattr(obj, 'all', None)): |
