【发布时间】:2018-08-01 05:05:18
【问题描述】:
给定一个名为 mattermost 的 django 应用程序,它有一个名为 Channel 的模型,我们可以做这样的事情。
import mattermost
for channel in Channel.objects.all():
print(channel)
我希望能够做这样的事情
import mattermost
mattermost.channels.list
我尝试在与 mattermost/init.py 相同的文件夹中添加带有 def list(): 函数的 channels.py。
我收到以下错误。
In [7]: reload(mattermost)
Out[7]: <module 'mattermost' from '/home/csmu/mipgen-django/mattermost/__init__.py'>
In [8]: mattermost.channels.list
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-d4715777f4f1> in <module>()
----> 1 mattermost.channels.list
AttributeError: module 'mattermost' has no attribute 'channels'
如何为 django app python 模块添加属性?
channels.py的内容:
import mattermost
def list():
for channel in mattermost.models.Channel.objects.all():
print(channel)
【问题讨论】: