Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with string args in GenericRelation. #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions sphinxcontrib_django/docstrings/field_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ def get_field_verbose_name(field):
related_name = (
related_name or field.remote_field.model._meta.verbose_name_plural
)
# If field is a foreign key or a ManyToMany field, use the prefix "All"
verbose_name = (
f"All {related_name} of this {field.model._meta.verbose_name}"
# handle potential str-type deferred model names
forward_name = (
field.model
if isinstance(field.model, str)
else field.model._meta.verbose_name
)
# If field is a foreign key or a ManyToMany field, use the prefix "All"
verbose_name = f"All {related_name} of this {forward_name}"
# Link to the origin of the reverse related field if it's not from an abstract model
if not field.remote_field.model._meta.abstract:
verbose_name += (
Expand Down
1 change: 1 addition & 0 deletions tests/roots/test-docstrings/dummy_django_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"django.contrib.auth",
"django.contrib.contenttypes",
"dummy_django_app",
"dummy_django_app2",
]

USE_TZ = False
7 changes: 7 additions & 0 deletions tests/roots/test-docstrings/dummy_django_app2/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models


class GenericRelationModel(models.Model):
# specifically test deferred string-type argument
relation_field = GenericRelation("dummy_django_app.TaggedItem")
25 changes: 25 additions & 0 deletions tests/test_attribute_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,28 @@ def test_phonenumber_field(app, do_autodoc):
" Phone number",
"",
]


@pytest.mark.sphinx("html", testroot="docstrings")
def test_generic_relation_field(app, do_autodoc):
actual = do_autodoc(
app, "attribute", "dummy_django_app2.models.GenericRelationModel.relation_field"
)
print(actual)
assert list(actual) == [
"",
".. py:attribute:: GenericRelationModel.relation_field",
" :module: dummy_django_app2.models",
"",
(
" Type: Reverse"
" :class:`~django.contrib.contenttypes.fields.GenericRelation` from"
" :class:`~dummy_django_app2.models.GenericRelationModel`"
),
"",
(
" All + of this tagged item (related name of"
" :attr:`~dummy_django_app2.models.GenericRelationModel.relation_field`)"
),
"",
]
27 changes: 27 additions & 0 deletions tests/test_class_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,30 @@ def test_form(app, do_autodoc):
" * ``test2``: Test2 (:class:`~django.forms.CharField`)",
"",
]


@pytest.mark.sphinx("html", testroot="docstrings")
def test_relation_model(app, do_autodoc):
actual = do_autodoc(app, "class", "dummy_django_app2.models.GenericRelationModel")
print(actual)
assert list(actual) == [
"",
".. py:class:: GenericRelationModel(id)",
" :module: dummy_django_app2.models",
"",
" :param id: Primary key: ID",
" :type id: ~django.db.models.AutoField",
"",
" Relationship fields:",
"",
(
" :param relation_field: Relation field (related name:"
" :attr:`~dummy_django_app.models.TaggedItem.+`)"
),
(
" :type relation_field:"
" :class:`~django.contrib.contenttypes.fields.GenericRelation` to"
" :class:`~dummy_django_app.models.TaggedItem`"
),
"",
]
6 changes: 3 additions & 3 deletions tests/test_data_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ def test_data(app, do_autodoc):
" :module: dummy_django_app.settings",
(
" :value: ['django.contrib.auth', 'django.contrib.contenttypes',"
" 'dummy_django_app']"
" 'dummy_django_app', 'dummy_django_app2']"
),
"",
" These are the installed apps",
"",
" .. code-block:: JavaScript",
"",
(
" ['django.contrib.auth', 'django.contrib.contenttypes',"
" 'dummy_django_app']\n"
" [\n 'django.contrib.auth',\n 'django.contrib.contenttypes',\n"
" 'dummy_django_app',\n 'dummy_django_app2',\n]\n"
),
"",
]