1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #24917 -- Made admindocs display model methods that take arguments.

This commit is contained in:
Zan Anderle
2014-10-30 20:45:01 +01:00
committed by Tim Graham
parent 3c5862ccb0
commit f3dc173240
8 changed files with 201 additions and 14 deletions

View File

@@ -27,6 +27,7 @@
{{ description }}
<h3>{% trans 'Fields' %}</h3>
<div class="module">
<table class="model">
<thead>
@@ -48,6 +49,30 @@
</table>
</div>
{% if methods %}
<h3>{% trans 'Methods with arguments' %}</h3>
<div class="module">
<table class="model">
<thead>
<tr>
<th>{% trans 'Method' %}</th>
<th>{% trans 'Arguments' %}</th>
<th>{% trans 'Description' %}</th>
</tr>
</thead>
<tbody>
{% for method in methods|dictsort:"name" %}
<tr>
<td>{{ method.name }}</td>
<td>{{ method.arguments }}</td>
<td>{{ method.verbose }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
<p class="small"><a href="{% url 'django-admindocs-models-index' %}">&lsaquo; {% trans 'Back to Model Documentation' %}</a></p>
</div>
{% endblock %}

View File

@@ -14,7 +14,10 @@ from django.db import models
from django.http import Http404
from django.template.engine import Engine
from django.utils.decorators import method_decorator
from django.utils.inspect import func_has_no_args
from django.utils.inspect import (
func_accepts_kwargs, func_accepts_var_args, func_has_no_args,
get_func_full_args,
)
from django.utils.translation import ugettext as _
from django.views.generic import TemplateView
@@ -219,7 +222,7 @@ class ModelDetailView(BaseAdminDocsView):
fields.append({
'name': field.name,
'data_type': data_type,
'verbose': verbose,
'verbose': verbose or '',
'help_text': field.help_text,
})
@@ -242,9 +245,10 @@ class ModelDetailView(BaseAdminDocsView):
'verbose': utils.parse_rst(_("number of %s") % verbose, 'model', _('model:') + opts.model_name),
})
methods = []
# Gather model methods.
for func_name, func in model.__dict__.items():
if inspect.isfunction(func) and func_has_no_args(func):
if inspect.isfunction(func):
try:
for exclude in MODEL_METHODS_EXCLUDE:
if func_name.startswith(exclude):
@@ -254,11 +258,29 @@ class ModelDetailView(BaseAdminDocsView):
verbose = func.__doc__
if verbose:
verbose = utils.parse_rst(utils.trim_docstring(verbose), 'model', _('model:') + opts.model_name)
fields.append({
'name': func_name,
'data_type': get_return_data_type(func_name),
'verbose': verbose,
})
# If a method has no arguments, show it as a 'field', otherwise
# as a 'method with arguments'.
if func_has_no_args(func) and not func_accepts_kwargs(func) and not func_accepts_var_args(func):
fields.append({
'name': func_name,
'data_type': get_return_data_type(func_name),
'verbose': verbose or '',
})
else:
arguments = get_func_full_args(func)
print_arguments = arguments
# Join arguments with ', ' and in case of default value,
# join it with '='. Use repr() so that strings will be
# correctly displayed.
print_arguments = ', '.join([
'='.join(list(arg_el[:1]) + [repr(el) for el in arg_el[1:]])
for arg_el in arguments
])
methods.append({
'name': func_name,
'arguments': print_arguments,
'verbose': verbose or '',
})
# Gather related objects
for rel in opts.related_objects:
@@ -282,6 +304,7 @@ class ModelDetailView(BaseAdminDocsView):
'summary': title,
'description': body,
'fields': fields,
'methods': methods,
})
return super(ModelDetailView, self).get_context_data(**kwargs)