sphinx-doc / sphinxcontrib-django

This is a sphinx extension which improves the documentation of Django apps.
https://pypi.org/project/sphinxcontrib-django/
Apache License 2.0
45 stars 25 forks source link

exception: 'str' object has no attribute '_meta' when GenericRelation #43

Open ntouran opened 1 year ago

ntouran commented 1 year ago

If you have a GenericRelation field that uses a deferred string argument rather than an a class argument, documentation build fails with:

Extension error (sphinxcontrib_django.docstrings):
Handler <function improve_docstring at 0x00000123AE286EE0> for event 'autodoc-process-docstring' threw an exception (exception: 'str' object has no attribute '_meta')

Through the debugger, I narrowed the issue down to here, where the format string argument fails because field.model is a string ("app2.ModelB" in the example code below)

https://github.com/edoburu/sphinxcontrib-django/blob/872cb8d56df8d76315a233ba8a2af3d9cce23b1c/sphinxcontrib_django/docstrings/field_utils.py#L83

Example code:

# in app1/models.py
class ModelA(models.Model):
    relation_field = GenericRelation(
        "app2.ModelB",
    )

# in app2/models.py
class ModelB(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField(null=True)
    content_object = GenericForeignKey()

I have a fix incoming that deals with this.