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

AttributeError: 'ForeignKey' object has no attribute 'rel' #2

Closed alexgleason closed 6 years ago

alexgleason commented 6 years ago

When building Sphinx html docs with the plugin enabled I get this error:

Exception occurred:
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinxcontrib_django/docstrings.py", line 143, in _add_model_fields_as_params
    to = field.rel.to
AttributeError: 'ForeignKey' object has no attribute 'rel'

EDIT: Here is the full traceback

# Sphinx version: 1.6.6
# Python version: 3.5.2 (CPython)
# Docutils version: 0.14 
# Jinja2 version: 2.10
# Last messages:
#   updating environment:
#   
#   3 added, 0 changed, 0 removed
#   
#   reading sources... [ 33%] index
#   
#   reading sources... [ 66%] local-development
#   
#   reading sources... [100%] models
#   
# Loaded extensions:
#   alabaster (0.7.10) from /home/alex/.virtualenvs/gh/lib/python3.5/site-packages/alabaster/__init__.py
#   sphinxcontrib_django (unknown version) from /home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinxcontrib_django/__init__.py
#   sphinx.ext.autodoc (1.6.6) from /home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/autodoc.py
#   sphinx.ext.coverage (1.6.6) from /home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/coverage.py
Traceback (most recent call last):
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/cmdline.py", line 306, in main
    app.build(opts.force_all, filenames)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/application.py", line 339, in build
    self.builder.build_update()
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/builders/__init__.py", line 329, in build_update
    'out of date' % len(to_build))
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/builders/__init__.py", line 342, in build
    updated_docnames = set(self.env.update(self.config, self.srcdir, self.doctreedir))
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/environment/__init__.py", line 601, in update
    self._read_serial(docnames, self.app)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/environment/__init__.py", line 621, in _read_serial
    self.read_doc(docname, app)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/environment/__init__.py", line 758, in read_doc
    pub.publish()
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/core.py", line 217, in publish
    self.settings)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/io.py", line 74, in read
    self.parse()
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/readers/__init__.py", line 78, in parse
    self.parser.parse(self.input, document)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/__init__.py", line 191, in parse
    self.statemachine.run(inputlines, document, inliner=self.inliner)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 171, in run
    input_source=document['source'])
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/statemachine.py", line 239, in run
    context, state, transitions)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/statemachine.py", line 460, in check_line
    return method(match, context, next_state)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2753, in underline
    self.section(title, source, style, lineno - 1, messages)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 327, in section
    self.new_subsection(title, lineno, messages)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 395, in new_subsection
    node=section_node, match_titles=True)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 282, in nested_parse
    node=node, match_titles=match_titles)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 196, in run
    results = StateMachineWS.run(self, input_lines, input_offset)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/statemachine.py", line 239, in run
    context, state, transitions)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/statemachine.py", line 460, in check_line
    return method(match, context, next_state)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2326, in explicit_markup
    nodelist, blank_finish = self.explicit_construct(match)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2338, in explicit_construct
    return method(self, expmatch)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2081, in directive
    directive_class, match, type_name, option_presets)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2130, in run_directive
    result = directive_instance.run()
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 1843, in run
    documenter.generate(more_content=self.content)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 1132, in generate
    self.document_members(all_members)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 1053, in document_members
    check_module=members_check_module and not isattr)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 1540, in generate
    all_members=all_members)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 1129, in generate
    self.add_content(more_content)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 1523, in add_content
    ModuleLevelDocumenter.add_content(self, more_content)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 846, in add_content
    for i, line in enumerate(self.process_doc(docstrings)):
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 806, in process_doc
    self.options, docstringlines)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/application.py", line 489, in emit
    return self.events.emit(event, self, *args)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinx/events.py", line 79, in emit
    results.append(callback(*args))
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinxcontrib_django/docstrings.py", line 101, in improve_model_docstring
    _improve_class_docs(app, obj, lines)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinxcontrib_django/docstrings.py", line 114, in _improve_class_docs
    _add_model_fields_as_params(app, cls, lines)
  File "/home/alex/.virtualenvs/gh/lib/python3.5/site-packages/sphinxcontrib_django/docstrings.py", line 143, in _add_model_fields_as_params
    to = field.rel.to
AttributeError: 'ForeignKey' object has no attribute 'rel'
vdboor commented 6 years ago

Which version of Django are you using? This module has been tested on Django 1.11

alexgleason commented 6 years ago

Hi, I'm using Django 2.0

vdboor commented 6 years ago

Ah, it seems that field.rel should be field. remote_field instead. I haven't tested this app on 2.0 yet, but you can provide a pull request that fixes Django 2.0 compatibility (and keeps 1.11 compat)

atodorov commented 6 years ago

@vdboor I did change this to field.remote_field.model, fixed another typo unrelated to this and I still see errors about missing name and model attributes. I think these are happening then the FK relation is specified as string not as a model.

I'd love to contribute and make some fixes but it looks like you don't have any tests at all and you claim the package is production stable and working with Django 2.0 while it clearly isn't. Without tests I really have no idea what I'm changing in the code and if I'm not breaking anything else.

mauler commented 6 years ago

@atodorov can you send me the fixes? I am also using django 2.0

atodorov commented 6 years ago

@mauler I didn't manage to complete them b/c there were other unrelated issues and I didn't have time for them. I will get back to this once I really need it for my project, which is to say not probably very soon.

vdboor commented 6 years ago

This is now fixed in the master branch (see 6525fb014f88d7eefe0ac1a00c0d19a0ea3a55c8). I'm a bit cautious to release a new version already because I haven't tested this code on a full Django 2.0 project yet. If one of you could do that, I'd be really happy about it!

The first unit testing code was added as well, something that is really needed!

vdboor commented 6 years ago

Nevermind -- after adding some tests I've released v0.3!