Skip to content

Commit

Permalink
Fixed #32421 -- Made admindocs ModelDetailView show model cached prop…
Browse files Browse the repository at this point in the history
…erties.
  • Loading branch information
ramonsaraiva authored and felixxm committed Feb 11, 2021
1 parent 4372233 commit dcb094a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
10 changes: 6 additions & 4 deletions django/contrib/admindocs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.template.engine import Engine
from django.urls import get_mod_func, get_resolver, get_urlconf
from django.utils.decorators import method_decorator
from django.utils.functional import cached_property
from django.utils.inspect import (
func_accepts_kwargs, func_accepts_var_args, get_func_full_args,
method_has_no_args,
Expand Down Expand Up @@ -250,7 +251,7 @@ def get_context_data(self, **kwargs):
methods = []
# Gather model methods.
for func_name, func in model.__dict__.items():
if inspect.isfunction(func) or isinstance(func, property):
if inspect.isfunction(func) or isinstance(func, (cached_property, property)):
try:
for exclude in MODEL_METHODS_EXCLUDE:
if func_name.startswith(exclude):
Expand All @@ -261,9 +262,10 @@ def get_context_data(self, **kwargs):
verbose = verbose and (
utils.parse_rst(cleandoc(verbose), 'model', _('model:') + opts.model_name)
)
# Show properties and methods without arguments as fields.
# Otherwise, show as a 'method with arguments'.
if isinstance(func, property):
# Show properties, cached_properties, and methods without
# arguments as fields. Otherwise, show as a 'method with
# arguments'.
if isinstance(func, (cached_property, property)):
fields.append({
'name': func_name,
'data_type': get_return_data_type(func_name),
Expand Down
4 changes: 4 additions & 0 deletions docs/ref/contrib/admin/admindocs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ system along with all the fields, properties, and methods available on it.
Relationships to other models appear as hyperlinks. Descriptions are pulled
from ``help_text`` attributes on fields or from docstrings on model methods.

.. versionchanged:: 4.0

Older versions don't display model cached properties.

A model with useful documentation might look like this::

class BlogEntry(models.Model):
Expand Down
2 changes: 2 additions & 0 deletions docs/releases/4.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Minor features
* The admindocs now allows esoteric setups where :setting:`ROOT_URLCONF` is not
a string.

* The model section of the ``admindocs`` now shows cached properties.

:mod:`django.contrib.auth`
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions tests/admin_docs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from django.db import models
from django.utils.functional import cached_property


class Company(models.Model):
Expand Down Expand Up @@ -56,6 +57,10 @@ def dummy_function(self, baz, rox, *some_args, **some_kwargs):
def a_property(self):
return 'a_property'

@cached_property
def a_cached_property(self):
return 'a_cached_property'

def suffix_company_name(self, suffix='ltd'):
return self.company.name + suffix

Expand Down
4 changes: 4 additions & 0 deletions tests/admin_docs/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ def test_instance_of_property_methods_are_displayed(self):
"""Model properties are displayed as fields."""
self.assertContains(self.response, '<td>a_property</td>')

def test_instance_of_cached_property_methods_are_displayed(self):
"""Model cached properties are displayed as fields."""
self.assertContains(self.response, '<td>a_cached_property</td>')

def test_method_data_types(self):
company = Company.objects.create(name="Django")
person = Person.objects.create(first_name="Human", last_name="User", company=company)
Expand Down

0 comments on commit dcb094a

Please sign in to comment.