The docs on using to_representation
is somewhat short. This method is used by Django Rest Framework 3.0+
to change the representation of your data in an API.
Here' the documentation link:
Here is my current code:
from django.forms.models import model_to_dict
class PersonListSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = ('foo', 'bar',)
def to_representation(self, instance):
return model_to_dict(instance)
When I do this code, it returns all fields in the model instead of the fields that I have specified above in class Meta: fields
.
Is it possible to reference the class Meta: fields
within the to_representation
method?
self._readable_fields
in the source. github.com/tomchristie/django-rest-framework/blob/master/… – Mantooth