diff options
| author | Mark Shirley | 2013-01-08 08:40:00 -0800 |
|---|---|---|
| committer | Mark Shirley | 2013-01-08 08:40:00 -0800 |
| commit | 394a26f8330c99cc7b9297bde0d56eed7586964a (patch) | |
| tree | 315f5d7d05011e260037cd1c2b53c4a60b1d85fd /rest_framework/serializers.py | |
| parent | 431ced66e49905fd76db0c36f62794dc3f42470b (diff) | |
| parent | b298bf53f310fae48083d87408d69e2d46e60671 (diff) | |
| download | django-rest-framework-394a26f8330c99cc7b9297bde0d56eed7586964a.tar.bz2 | |
Merge pull request #553 from maspwr/null-one-to-one
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 19a955b8..da0af467 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -298,15 +298,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)): |
