给定一个名为whatmost的django应用程序,它具有一个名为Channel的模型,我们可以执行以下操作。
import mattermost
for channel in Channel.objects.all():
print(channel)
我希望能够做这样的事情
import mattermost
mattermost.channels.list
我试过用def list():将channels.py添加到与somethingmost / init.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应用python模块?
channel.py的内容:
import mattermost
def list():
for channel in mattermost.models.Channel.objects.all():
print(channel)
最佳答案
尝试from mattermost import channelsprint(channels.list())
结果是:
In [1]: import mattermost
In [2]: mattermost.channels.list()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-249466f32547> in <module>()
----> 1 mattermost.channels.list()
AttributeError: module 'mattermost' has no attribute 'channels'
In [3]: from mattermost import channels
In [4]: mattermost.channels.list()
list.stub
In [5]:
这很接近。