我正在使用Sphinx,并尝试为我的Django项目生成文档。我决定首先尝试记录模型,因此在我的.rst文件中,我这样做:
wdland\.models
==============
.. automodule:: wdland.models
:members:
:undoc-members:
:show-inheritance:
但是出现以下错误:
WARNING: /home/fabou/wdlandenvpy3/source/docs/wdland.rst:9: (WARNING/2) autodoc: failed to import module 'wdland.models'; the following exception was raised:
Traceback (most recent call last):
File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 657, in import_object
__import__(self.modname)
File "/home/fabou/wdlandenvpy3/source/wdland/models.py", line 35, in <module>
class Device(models.Model):
File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/db/models/base.py", line 118, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class wdland.models.Device doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
在进行一些谷歌搜索后,我在这里找到了一个解决方案Django 1.9 deprecation warnings app_label,请看“Jan 11 '16 at 14:54”下来自Роман Арсеньев的评论。
我进入models.py并按照上述帖子向每个类添加“class Meta:app_label”,以解决问题。这样我就能够使用sphinx-build脚本生成我的模型文档。
现在我想在views.py中做同样的事情,并在我的.rst文件中执行以下操作。
wdland\.views
=============
.. automodule:: wdland.views
:members:
:undoc-members:
:show-inheritance:
但是在编译时,我遇到了一个新的类似错误,而且我一直无法找到解决方法,也没有理解之前对模型进行修复的方法。
WARNING: /home/fabou/wdlandenvpy3/source/docs/wdland.rst:19: (WARNING/2) autodoc: failed to import module 'wdland.views'; the following exception was raised:
Traceback (most recent call last):
File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 657, in import_object
__import__(self.modname)
File "/home/fabou/wdlandenvpy3/source/wdland/views.py", line 2, in <module>
from . import views
File "/home/fabou/wdlandenvpy3/source/wdland/views.py", line 19, in <module>
from .forms import TicketForm, TicketAmendForm
File "/home/fabou/wdlandenvpy3/source/wdland/forms.py", line 1, in <module>
from django.contrib.auth.forms import AuthenticationForm
File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/contrib/auth/forms.py", line 12, in <module>
from django.contrib.auth.models import User
File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/contrib/auth/models.py", line 6, in <module>
from django.contrib.contenttypes.models import ContentType
File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 139, in <module>
class ContentType(models.Model):
File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/db/models/base.py", line 118, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
这里是相关的Sphinx配置。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import django
import os
import sphinx_rtd_theme
import sys
from django.conf import settings
sys.path.insert(0, os.path.abspath('../'))
settings.configure()
django.setup()
wdland是Django应用程序,并在设置文件中列在我的INSTALLED_APPS元组中。sphinx文档文件夹与wdland文件夹处于同一级别。
阅读类似的主题线似乎指向在调用时尚未加载的模块。我不知道如何修复,因为对sphinx和Django都没有经验。你有任何想法可以帮助我解决吗?
尝试记录urls.py也会产生类似的错误。
编辑: 这里是我的view.py开头
import datetime
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import Http404, HttpResponse
from django.template.loader import render_to_string
from django.shortcuts import render, HttpResponseRedirect
from django.views import generic
from random import randint
from script.serversnmp import get_ubuntu_snmp, get_esxi_snmp
from script.wdlandtools import get_monthly_sess_hour_stats,\
get_monthly_sess_nb_stats,\
get_monthly_ticket_stats,\
get_ticket_category_stats,\
clean_usersession_tbl, update_usertotals_tbl,\
send_email_notification, get_adsl_usage
from wdland.models import Device, UserSession, SupportTicket, News, UserTotals,\
SavedUsrSess
from .forms import TicketForm, TicketAmendForm
from jchart import Chart
from jchart.config import Axes, DataSet, rgba