我正在使用Google Firestore。这是我的自定义对象
class Post(object):
def __init__(self, Author, Message, Date):
self.Author = Author
self.Message = Message
self.Date = Date
@staticmethod
def from_dict(source):
# [START_EXCLUDE]
post = Post(source[u'Author'], source[u'Message'], source[u'Date'])
if u'Author' in source:
post.Author = source[u'Author']
if u'Message' in source:
post.Message = source[u'Message']
if u'Date' in source:
post.Date = source[u'Date']
return post
# [END_EXCLUDE]
def to_dict(self):
# [START_EXCLUDE]
dest = {
u'Author':self.Author,
u'Message':self.Message,
u'Date':self.Date
}
if self.Author:
dest[u'Author'] = self.Author
if self.Message:
dest[u'Message'] = self.Message
if self.Date:
dest[u'Date'] = self.Date
return dest
#[END_EXCLUDE]
def __repr__(self):
return(
u'Post(Author={}, Message={}, Date={})'
.format(self.Author, self.Message, self.Date))
我的main.py Flask代码的一部分
from flask import Flask
from google.cloud import firestore
from models.posts.post import Post
app = Flask(__name__)
db = firestore.Client()
@app.route('/')
def hello():
posts_ref = db.collection(u'Posts')
doc_ref = posts_ref.get()
posts = Post.from_dict(doc_ref)
retval = "Empty"
for post in list(posts):
retval = """Post ID : {}\n
Author : {}\n
Message : {}""".format(post.Author,
post.Date,
post.Message)
运行Flask应用程序时,出现以下错误。
[2019-02-23 09:20:42 +0000] [502] [INFO] Listening at: http://0.0.0.0:24197 (502)
[2019-02-23 09:20:42 +0000] [502] [INFO] Using worker: sync
File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/tmp/tmpszrPxY/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vinay/site-web/main.py", line 12, in hello
posts = Post.from_dict(doc_ref)
File "/home/vinay/site-web/models/posts/post.py", line 10, in from_dict
post = Post(source[u'Author'], source[u'Message'], source[u'Date'])
TypeError: 'generator' object is not subscriptable
INFO 2019-02-23 09:20:49,854 module.py:861] default: "GET /?authuser=0 HTTP/1.1" 500 291