Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion rest_framework_docs/api_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from rest_framework.viewsets import ModelViewSet
from rest_framework.serializers import BaseSerializer
from rest_framework.serializers import PrimaryKeyRelatedField

VIEWSET_METHODS = {
'List': ['get', 'post'],
Expand Down Expand Up @@ -122,13 +123,33 @@ def __get_serializer_fields__(self, serializer):
"type": str(field.__class__.__name__),
"sub_fields": sub_fields,
"required": field.required,
"to_many_relation": to_many_relation
"to_many_relation": to_many_relation,
"choices": self.__get_field_choices(field),
})
# FIXME:
# Show more attibutes of `field`?

return fields

def __get_field_choices(self, field):
"""If it's a related field returns an one element list with the
related model. If the field has choices returns the
choices. Else returns `None`

"""
if isinstance(field, PrimaryKeyRelatedField):
return ['Related field ' + str(field.queryset.model)]

if hasattr(field, 'choices'):
# Force choice keys to be a string or `json.dumps` fails
# This happens when using django-timezone-field where
# choices are a <pytz.timezone> object.
if isinstance(field.choices, dict):
if not all(map(lambda x: isinstance(x, str), field.choices.keys())):
choices = dict([(str(key), field.choices[key]) for key in field.choices.keys()])
return choices
return field.choices

def __get_serializer_fields_json__(self):
# FIXME:
# Return JSON or not?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
<span class="label label-primary label-required" title="Support sending several objects of this type in a request ({{obj}, {obj}, ... })">Array of objects</span>
{% endif %}

{% if field.choices %}
<p>Choices
<ul>
{% for choice in field.choices %}
<li>{{ choice }}</li>
{% endfor %}
</ul>
</p>
{% endif %}

{% if field.sub_fields %}
{%include "rest_framework_docs/components/fields_list.html" with fields=field.sub_fields %}
{% endif %}
Expand Down