python3 / pymongo“TypeError:'类中的'NoneType'对象不可订阅”

时间:2016-12-29 17:40:28

标签: python mongodb python-3.x pymongo

与一些python一起玩,新的和敏锐但感觉失败。 任何帮助将不胜感激。我试图从烧瓶应用程序中查询mongo,我似乎丢失了某些内容或执行了非法操作:

local_settings = Database.find_sort(AdminConstants.COLLECTION, "admin.created_date", -1, 1)

它位于类下方,似乎返回None。这不应该是这样的情况,因为运行命令(没有类)在命令行上返回一个值。我最后会加上这个,希望有人可以为我解决这个问题。在课外移动它仍然会导致错误 - 非常令人沮丧的是看到它在终端上工作并在我需要的地方失败。

编辑:数据库当然在程序的这一点初始化。 编辑2:永远不要太确定自己;)

代码: database.py

import pymongo
class Database(object):
    URI = "mongodb://127.0.0.1:27017"
    DATABASE = None

    @staticmethod
    def initialize():
        client = pymongo.MongoClient(Database.URI)
        Database.DATABASE = client['cvm']

    @staticmethod
    def find_sort(collection, query, direction, limit):
        return Database.DATABASE[collection].find({}).sort(query, direction).limit(limit)

admin.py

from src.common.database import Database

class Admin(object):
    local_settings = Database.find_sort(AdminConstants.COLLECTION, "admin.created_date", -1, 1)

错误输出:

Traceback (most recent call last):
  File "/home/frank/PycharmProjects/cvm/src/run.py", line 1, in <module>
    from src.app import app
  File "/home/frank/PycharmProjects/cvm/src/app.py", line 19, in <module>
    from src.models.admin.views import admin_blueprint
  File "/home/frank/PycharmProjects/cvm/src/models/admin/views.py", line 4, in <module>
    from src.models.admin.admin import Admin
  File "/home/frank/PycharmProjects/cvm/src/models/admin/admin.py", line 6, in <module>
    class Admin(object):
  File "/home/frank/PycharmProjects/cvm/src/models/admin/admin.py", line 8, in Admin
    local_settings = Database.find_sort(AdminConstants.COLLECTION, "admin.created_date", -1, 1)
  File "/home/frank/PycharmProjects/cvm/src/common/database.py", line 27, in find_sort
    return (Database.DATABASE[collection].find({}).sort(query, direction).limit(limit))
TypeError: 'NoneType' object is not subscriptable

终端:

Python 3.4.3 (default, Aug  9 2016, 15:36:17)
>>> import pymongo
>>> URI = "mongodb://127.0.0.1:27017"
>>> client = pymongo.MongoClient(URI)
>>> DATABASE = client['cvm']
>>> def find_sort(collection, query, direction, limit):
...     return DATABASE[collection].find().sort(query, direction).limit(limit)
... 
>>> COLLECTION = "admin"
>>> tmp1 = find_sort(COLLECTION, "created_date", -1, 1)
>>> list(tmp1)
[{'created_date': '20161212', '_id': ObjectId('5863f28d101bbd88b70be948')}]

谢谢你的时间。

2 个答案:

答案 0 :(得分:0)

您需要创建Database课程的实例才能调用find_sort。然后,您可以使用python self keyword

class Database(object):
    URI = "mongodb://127.0.0.1:27017"
    DATABASE = None

    @staticmethod
    def initialize(self):
        client = pymongo.MongoClient(self.URI)
        self.DATABASE = client['cvm']

    @staticmethod
    def find_sort(self, collection, query, direction, limit):
        return self.DATABASE[collection].find({}).sort(query, direction).limit(limit)

那么这应该有效:

Database().find_sort(AdminConstants.COLLECTION, "admin.created_date", -1, 1)

答案 1 :(得分:0)

丹尼尔罗斯曼发现数据库没有初始化。我相信这可以通过之前的电话进行,但在课堂上是必需的。

Binary representation of 50.200000:
9a 99 99 99 99 19 49 40

Integer (nonsense) representation of 50.200000:
-1717986918