mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
Nice manager representations on serializer classes
This commit is contained in:
parent
ab40780dc2
commit
bf52d04f4c
|
@ -2,10 +2,23 @@
|
||||||
Helper functions for creating user-friendly representations
|
Helper functions for creating user-friendly representations
|
||||||
of serializer classes and serializer fields.
|
of serializer classes and serializer fields.
|
||||||
"""
|
"""
|
||||||
|
from django.db import models
|
||||||
import re
|
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):
|
def smart_repr(value):
|
||||||
|
if isinstance(value, models.Manager):
|
||||||
|
return manager_repr(value)
|
||||||
|
|
||||||
value = repr(value)
|
value = repr(value)
|
||||||
|
|
||||||
# Representations like u'help text'
|
# Representations like u'help text'
|
||||||
|
|
|
@ -81,9 +81,9 @@ class RelationalModel(models.Model):
|
||||||
RELATIONAL_FLAT_REPR = """
|
RELATIONAL_FLAT_REPR = """
|
||||||
TestSerializer():
|
TestSerializer():
|
||||||
id = IntegerField(label='ID', read_only=True)
|
id = IntegerField(label='ID', read_only=True)
|
||||||
foreign_key = PrimaryKeyRelatedField(label='foreign key', queryset=<django.db.models.manager.Manager object>)
|
foreign_key = PrimaryKeyRelatedField(label='foreign key', queryset=ForeignKeyTargetModel.objects.all())
|
||||||
one_to_one = PrimaryKeyRelatedField(label='one to one', queryset=<django.db.models.manager.Manager object>)
|
one_to_one = PrimaryKeyRelatedField(label='one to one', queryset=OneToOneTargetModel.objects.all())
|
||||||
many_to_many = PrimaryKeyRelatedField(label='many to many', many=True, queryset=<django.db.models.manager.Manager object>)
|
many_to_many = PrimaryKeyRelatedField(label='many to many', many=True, queryset=ManyToManyTargetModel.objects.all())
|
||||||
""".strip()
|
""".strip()
|
||||||
|
|
||||||
|
|
||||||
|
@ -105,9 +105,9 @@ TestSerializer():
|
||||||
HYPERLINKED_FLAT_REPR = """
|
HYPERLINKED_FLAT_REPR = """
|
||||||
TestSerializer():
|
TestSerializer():
|
||||||
url = HyperlinkedIdentityField(view_name='relationalmodel-detail')
|
url = HyperlinkedIdentityField(view_name='relationalmodel-detail')
|
||||||
foreign_key = HyperlinkedRelatedField(label='foreign key', queryset=<django.db.models.manager.Manager object>, view_name='foreignkeytargetmodel-detail')
|
foreign_key = HyperlinkedRelatedField(label='foreign key', queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail')
|
||||||
one_to_one = HyperlinkedRelatedField(label='one to one', queryset=<django.db.models.manager.Manager object>, view_name='onetoonetargetmodel-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=<django.db.models.manager.Manager object>, view_name='manytomanytargetmodel-detail')
|
many_to_many = HyperlinkedRelatedField(label='many to many', many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail')
|
||||||
""".strip()
|
""".strip()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user