[英]Sphinx autodoc django not working
I've started using Sphinx for a django project, and I've hit a brick wall: 我已经开始使用Sphinx进行django项目了,我碰到了一堵砖墙:
The modules are documented with something like this: 模块记录如下:
:mod:`models` Module
--------------------
.. automodule:: userprofile.models
:members:
:undoc-members:
:show-inheritance:
The relevant code looks like this: 相关代码如下所示:
# models is django.db.models
class ProfileQuerystring(models.Model):
[..]
The problem is that ProfileQuerystring
does not appear in the HTML build. 问题是
ProfileQuerystring
没有出现在HTML构建中。 If, however, I remove the inheritance to models.Model
(so the line looks like class ProfileQuerystring:
) and re-build, the class gets documented. 但是,如果我将继承移除到
models.Model
(因此该行看起来像class ProfileQuerystring:
并重新构建, class ProfileQuerystring:
记录该类。
This doesn't happen a few lines above that code, where I inherit from models.Manager
. 这不会发生在代码之上的几行,我从
models.Manager
继承。
Can anyone help me out or at least give me a hint? 任何人都可以帮助我或者至少给我一个暗示吗?
LATER EDIT : 后期编辑 :
If I manually add it, it works: 如果我手动添加它,它的工作原理:
.. automodule:: cinely.userprofile.models
:members:
:undoc-members:
:show-inheritance:
.. autoclass:: cinely.userprofile.models.ProfileQuerystring # <-- note this
So the class can be documented, but somehow automodule
doesn't want to. 因此可以记录该类,但不知何故,
automodule
不想这样做。
LATER EDIT 2 : 后期编辑2 :
I've tried removing the __metaclass__
attribute from Model
, but nothing happens. 我试过从
Model
删除__metaclass__
属性,但没有任何反应。 Also, since I've started editing the Django source, I took the chance to print something to the console, but nothing happened. 此外,由于我已经开始编辑Django源代码,我有机会在控制台上打印一些东西,但什么都没发生。 I can confirm that the customized django is used, because I've uninstalled the
pip
installed one. 我可以确认使用了自定义的django,因为我已经卸载了已安装的
pip
。
This is a tentative answer, and it might be wrong. 这是一个试探性的答案,可能是错的。 It was simply too long to be put as a comment... However, if you take a look at the django source you will notice that
Model
and Manager
have a different inheritance three. 它只是作为一个评论太长了......但是,如果你看一下django源代码,你会注意到
Model
和Manager
有一个不同的继承三。 In particular, the Model class uses the __metaclass__
attribute: 特别是, Model类使用
__metaclass__
属性:
class Manager(object):
# Tracks each time a Manager instance is created. Used to retain order.
creation_counter = 0
class Model(object):
__metaclass__ = ModelBase
_deferred = False
My guess (but it's nothing more than that: a guess) is that when ModelBase
creates the class, it manipulate the __module__
attribute in a way that confuses sphinx and makes impossible for it to understand that the class is part of the module models
. 我的猜测(但不过是一个猜测)是当
ModelBase
创建类时,它以一种混淆sphinx的方式操纵__module__
属性,使得它无法理解该类是模块models
一部分。
module = attrs.pop('__module__')
new_class = super_new(cls, name, bases, {'__module__': module})
This hypothesis is consistent with your observation that - when the class to document is specified explicitly - sphinx has no problem in generating the documentation. 这个假设与您的观察结果一致 - 当明确指定文档类时 - sphinx在生成文档时没有问题。
Does it help? 有帮助吗?
EDIT: Just occurred to me that a simple way to verify this hypothesis would be to temporary comment out the __metaclass__
line in the Model
class definition and see if this way sphinx picks it up... 编辑:我刚想到,验证这个假设的一个简单方法是临时注释掉
Model
类定义中的__metaclass__
行,看看sphinx是否采用了这种方式...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.