tomchristie/django-rest-framework — pull request #504 by dhendo (2012-12-13 16:36:57+00:00)
Title: Don't attempt to convert dictionary keys

I'm attempting to hook up rest framework to MongoEngine models.

After a bit of tweaking (MongoModelSerializer: https://gist.github.com/4277592), this works OK with a non-paginated Serializer.

Model:
```python
class Person(Document):
email = StringField(required=False)
did = StringField(required=False)
dt = DateTimeField(required=False)
fname = StringField(required=False)
lname = StringField(required=False)
_id = ObjectIdField(required=False, primary_key=True)
_default_manager = mongoengine.queryset.QuerySetManager()

meta = {
'allow_inheritance': False,
'ordering' : ['_id'],
}
```

View
```python
class PeopleList(generics.ListAPIView):
"""
A list of customers within the system
"""
model = Person
serializer_class = PersonSerializer
paginate_by = 10
pagination_serializer_class = PaginatedPersonSerializer
```

```python
class PaginatedPersonSerializer(BasePaginationSerializer):
next = pagination.NextPageField(source='*')
prev = pagination.PreviousPageField(source='*')
total_results = serializers.Field(source='paginator.count')
```

```python
class PersonSerializer(TMSModelSerializer):
class Meta:
model = Person
fields = ('_id','email', 'did', 'fname', 'lname', 'dt' )
pk = '_id'
```

When I hit the list of people, I get the error:

```
Traceback (most recent call last):
File "vendor/lib/python/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "vendor/lib/python/django/views/generic/base.py", line 48, in view
return self.dispatch(request, *args, **kwargs)
File "vendor/lib/python/django/views/decorators/csrf.py", line 77, in wrapped_view
return view_func(*args, **kwargs)
File "site-packages/rest_framework/views.py", line 363, in dispatch
response = self.handle_exception(exc)
File "site-packages/rest_framework/views.py", line 360, in dispatch
response = handler(request, *args, **kwargs)
File "apps/tmsapi/views.py", line 41, in get
return self.list(request, *args, **kwargs)
File "site-packages/rest_framework/mixins.py", line 65, in list
return Response(serializer.data)
File "site-packages/rest_framework/serializers.py", line 347, in data
self._data = self.to_native(self.object)
File "site-packages/rest_framework/pagination.py", line 72, in to_native
return self.convert_object(obj)
File "site-packages/rest_framework/serializers.py", line 218, in convert_object
value = field.field_to_native(obj, field_name)
File "site-packages/rest_framework/serializers.py", line 325, in field_to_native
return [self.to_native(item) for item in obj.all()]
File "site-packages/rest_framework/serializers.py", line 287, in to_native
return [self.convert_object(item) for item in obj]
File "site-packages/rest_framework/serializers.py", line 218, in convert_object
value = field.field_to_native(obj, field_name)
File "site-packages/rest_framework/fields.py", line 87, in field_to_native
value = getattr(obj, field_name)
AttributeError: 'str' object has no attribute '_id'	/
```

The problem appears to be in ```BaseSerializer to_native()``` - the obj passed in in my case is the MongoEngine Model - which is iterable.

Iterating over it will provide the string keys of the model. Adding the extra dictionary check avoids trying to convert strings.