Skip to content

Commit 04b50df

Browse files
author
Emmanouil Konstantinidis
committed
List all fields for serializer
1 parent fea55ee commit 04b50df

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

drfdocs/api_endpoint.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init__(self, pattern, parent_pattern=None):
1010
self.path = self.__get_path__(parent_pattern)
1111
self.allowed_methods = self.__get_allowed_methods__()
1212
self.view_name = pattern.callback.__name__
13+
self.fields = self.__get_serializer_fields__()
1314

1415
def __get_path__(self, parent_pattern):
1516
if parent_pattern:
@@ -19,3 +20,13 @@ def __get_path__(self, parent_pattern):
1920

2021
def __get_allowed_methods__(self):
2122
return [m.upper() for m in self.callback.cls.http_method_names if hasattr(self.callback.cls, m)]
23+
24+
def __get_serializer_fields__(self):
25+
fields = []
26+
27+
if hasattr(self.callback.cls, 'serializer_class') and hasattr(self.callback.cls.serializer_class, 'get_fields'):
28+
serializer = self.callback.cls.serializer_class
29+
if hasattr(serializer, 'get_fields'):
30+
fields = [{"name": key, "type": str(value.__class__.__name__)} for key, value in serializer().get_fields().items()]
31+
32+
return fields

drfdocs/templates/drfdocs/home.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
{% extends "drfdocs/base.html" %}
22

33
{% block content %}
4+
45
<h2>API Endpoints</h2>
56

67
{% for endpoint in endpoints %}
78
<div class="endpoint">
89
<h3 class="title">{{ endpoint.path }}</h3>
10+
911
<ul class="list-inline methods">
1012
{% for method in endpoint.allowed_methods %}
1113
<li class="method">{{ method }}</li>
1214
{% endfor %}
1315
</ul>
16+
1417
<p>View Name: {{ endpoint.view_name }}</p>
1518
<p>URL Name: {{ endpoint.name }}</p>
19+
20+
{% if endpoint.fields %}
21+
<p>Fields:</p>
22+
<ul class="list fields">
23+
{% for field in endpoint.fields %}
24+
<li class="field">{{ field.name }}: {{ field.type }}</li>
25+
{% endfor %}
26+
</ul>
27+
{% endif %}
28+
1629
</div>
1730
{% endfor %}
31+
1832
{% endblock %}

0 commit comments

Comments
 (0)