diff --git a/rest_framework/utils/representation.py b/rest_framework/utils/representation.py index 1a4d1a62c..71db18863 100644 --- a/rest_framework/utils/representation.py +++ b/rest_framework/utils/representation.py @@ -2,10 +2,23 @@ Helper functions for creating user-friendly representations of serializer classes and serializer fields. """ +from django.db import models import re +def manager_repr(value): + model = value.model + opts = model._meta + for _, name, manager in opts.concrete_managers + opts.abstract_managers: + if manager == value: + return '%s.%s.all()' % (model._meta.object_name, name) + return repr(value) + + def smart_repr(value): + if isinstance(value, models.Manager): + return manager_repr(value) + value = repr(value) # Representations like u'help text' diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index 57c97cb7d..6e9aaba6c 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -81,9 +81,9 @@ class RelationalModel(models.Model): RELATIONAL_FLAT_REPR = """ TestSerializer(): id = IntegerField(label='ID', read_only=True) - foreign_key = PrimaryKeyRelatedField(label='foreign key', queryset=) - one_to_one = PrimaryKeyRelatedField(label='one to one', queryset=) - many_to_many = PrimaryKeyRelatedField(label='many to many', many=True, queryset=) + foreign_key = PrimaryKeyRelatedField(label='foreign key', queryset=ForeignKeyTargetModel.objects.all()) + one_to_one = PrimaryKeyRelatedField(label='one to one', queryset=OneToOneTargetModel.objects.all()) + many_to_many = PrimaryKeyRelatedField(label='many to many', many=True, queryset=ManyToManyTargetModel.objects.all()) """.strip() @@ -105,9 +105,9 @@ TestSerializer(): HYPERLINKED_FLAT_REPR = """ TestSerializer(): url = HyperlinkedIdentityField(view_name='relationalmodel-detail') - foreign_key = HyperlinkedRelatedField(label='foreign key', queryset=, view_name='foreignkeytargetmodel-detail') - one_to_one = HyperlinkedRelatedField(label='one to one', queryset=, view_name='onetoonetargetmodel-detail') - many_to_many = HyperlinkedRelatedField(label='many to many', many=True, queryset=, view_name='manytomanytargetmodel-detail') + foreign_key = HyperlinkedRelatedField(label='foreign key', queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail') + one_to_one = HyperlinkedRelatedField(label='one to one', queryset=OneToOneTargetModel.objects.all(), view_name='onetoonetargetmodel-detail') + many_to_many = HyperlinkedRelatedField(label='many to many', many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail') """.strip()