4

I'm attempting to use sphinx to document the django app I'm writing. So far I have my code.rst setup to look at models.py, and when I run make html I get the automatic documentation, however I get a warning:

WARNING: autodoc can't import/find attribute 'myapp.models.MyModel.image', it reported error: "image", please check your spelling and sys.path

The entire tracelog is:

Traceback (most recent call last):
  File "C:\blah\lib\site-packages\sphinx\ext\autodoc.py", line 326, in import_object
    obj = self.get_attr(obj, part)
  File "C:\blah\lib\site-packages\sphinx\ext\autodoc.py", line 232, in get_attr
    return safe_getattr(obj, name, *defargs)
  File "C:\blah\lib\site-packages\sphinx\util\inspect.py", line 70, in safe_getattr
    raise AttributeError(name)
AttributeError: image

MyModel.image is an image field, simply on the model defined as:

#: image file location for ``MyModel``
image = models.ImageField(upload_to="images/")

If I change image to models.Charfield for example it runs fine. Is there any reason why an ImageField would cause sphinx issues?

my code.rst:

.. automodule:: dynamicbanners.models
   :members:
bad_coder
  • 11,289
  • 20
  • 44
  • 72
ptr
  • 3,292
  • 2
  • 24
  • 48
  • After further reading could this be anything to do with the fact that sphinx doesn't initialise the model class? Obviously all my attributes show up as ``my_attr = None`` in the docs because they weren't initialised, and I've [read answers](http://stackoverflow.com/a/14503035/2547709) that suggest this might be a bug? I'd be quite surprised that NO-ONE else has come across this issue yet. It seems oddly specific towards ``models.ImageField`` (Although having just tested, it also does this with ``models.FileField``. – ptr Nov 12 '13 at 17:06
  • added my code.rst just in case. Although right now I'd accept the answer if someone told me they had successfully managed to use autodoc on an ImageField before :) – ptr Nov 12 '13 at 17:44
  • @mzjn: That bug report link is broken now. (It prompts you to log in or create an account, then says Access Denied). – user9876 Jul 17 '15 at 13:14

2 Answers2

1

So after reading the comments it seems this is a quirk of Django that messes with sphinx.ext.autodoc. Hopefully a fix will be dropped into Django 1.6 soon, or failing that I think the upcoming 1.2 version of Sphinx might provide a way around it, but for now I'll need to find some sort of workaround.

ptr
  • 3,292
  • 2
  • 24
  • 48
  • Did you manage to fix this? I'm seeing exactly the same issue with https://pypi.python.org/pypi/jsonfield on Django 1.6.5 and sphinx 1.2.2. – Charl Botha Jul 04 '14 at 08:42
  • If anyone else is interested, the work-around for jsonfield is slightly different: https://bitbucket.org/birkenfeld/sphinx/issue/1254/autodoc-fails-to-handle-descriptors-with#comment-11070601 – Charl Botha Jul 04 '14 at 08:52
  • @CharlBotha: That bug report link is broken now. (It prompts you to log in or create an account, then says Access Denied). – user9876 Jul 17 '15 at 13:14
  • @user9876, new link to bug report: https://github.com/sphinx-doc/sphinx/issues/1254 – mzjn Jul 18 '15 at 05:16
0

Looks like this was fixed in Django 1.8 - https://code.djangoproject.com/ticket/12568

user9876
  • 10,954
  • 6
  • 44
  • 66