Repository: encode/django-rest-framework

Issue:
dotted source on CharField returns null from to_representation when allow_null=False
## Checklist

- [x] I have verified that that issue exists against the `master` branch of Django REST framework.
- [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- [x] This is not a usage question. (Those should be directed to the [discussion group](https://groups.google.com/forum/#!forum/django-rest-framework) instead.)
- [ ] This cannot be dealt with as a third party library. (We prefer new functionality to be [in the form of third party libraries](http://www.django-rest-framework.org/topics/third-party-resources/#about-third-party-packages) where possible.)
- [x] I have reduced the issue to the simplest possible case.
- [ ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

## Steps to reproduce
```
class PersonSerializer(serializers.Serializer):
    moms_name = serializers.CharField(
        allow_blank=True,
        allow_null=False,
        default='',
        required=False,
        source='mom_props.name'
)

```
The instance passed to the PersonSerializer may or may not have a mom_props model relationship in the database so getting the dotted source path may raise an ObjectDoesNotExist.

## Expected behavior
If mom_props.name were to return None or an ObjectDoesNotExist because in the case of django related models the mom_props model doesn't exist then moms_name should be an empty string in the output of `to_representation`

## Actual behavior
None or null is returned instead. This is inconsistent behavior for my api consumers since moms_name returns null but null is not a valid value ever for the property. If they turn around & submit the data back then they will get a validiationerror from the data I just sent them. This is probably related to #2299 but couldn't a better default be chosen in the case of CharFields with allow_blank=True & allow_null=False?

It's also quite likely I'm doing something really dumb but what I'm trying to accomplish is a mashup of multiple django models into a single serializer so my api consumers can interact with the data more easily without having to know or care about my backend schema. Pretty common I'm sure so it's more of a flattened serializer with a not flat database schema.

Everything works great when writing & naturally I've overridden the `create` & `update` methods & elected to be more explicit using the Serializer class instead of a ModelSerializer so it's more clear what is the API's responsibilty & what is the ORM's.

Finally, the part of the code base I think would need to be tweaked is:
https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L492-L501


Discussion:
Hi,

`allow_null` is only used for deserialization purposes. It's up to the developer to provide correct data for serialization as DRF will not perform check against since it's already supposed to be sane.

Is default only used for deserialization too? The docs say:

> When serializing the instance, default will be used if the the object attribute or dictionary key is not present in the instance.
@xordoquy - reopening this, since this seems like an edge case bug. It looks like serialization's `default` handling doesn't traverse relationships. As a quick test, I ran the following:

```python
class Serializer(serializers.Serializer):
    bar = serializers.CharField(
        allow_blank=True, 
        allow_null=False,
        default="",
        required=False, 
        source='foo.bar')
    
>>> Serializer({}).data
ReturnDict([('bar', '')])

>>> Serializer({'foo': {}}).data
ReturnDict([('bar', '')])

>>> Serializer({'foo': None}).data
ReturnDict([('bar', None)])
```
Thanks. What you just showed is what I experience as well.
Would you like me to update the issue description to better reflect default="" with dotted field sources serializes to null instead of empty string?

Resolving patch:
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -93,9 +93,6 @@ def get_attribute(instance, attrs):
     Also accepts either attribute lookup on objects or dictionary lookups.
     """
     for attr in attrs:
-        if instance is None:
-            # Break out early if we get `None` at any point in a nested lookup.
-            return None
         try:
             if isinstance(instance, collections.Mapping):
                 instance = instance[attr]


Test patch:
diff --git a/tests/test_serializer.py b/tests/test_serializer.py
--- a/tests/test_serializer.py
+++ b/tests/test_serializer.py
@@ -411,6 +411,19 @@ def test_default_not_used_when_in_object(self):
         serializer = self.Serializer(instance)
         assert serializer.data == {'has_default': 'def', 'has_default_callable': 'ghi', 'no_default': 'abc'}
 
+    def test_default_for_source_source(self):
+        """
+        'default="something"' should be used when a traversed attribute is missing from input.
+        """
+        class Serializer(serializers.Serializer):
+            traversed = serializers.CharField(default='x', source='traversed.attr')
+
+        assert Serializer({}).data == {'traversed': 'x'}
+        assert Serializer({'traversed': {}}).data == {'traversed': 'x'}
+        assert Serializer({'traversed': None}).data == {'traversed': 'x'}
+
+        assert Serializer({'traversed': {'attr': 'abc'}}).data == {'traversed': 'abc'}
+
 
 class TestCacheSerializerData:
     def test_cache_serializer_data(self):