本文整理汇总了Python中models.Post.to_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Post.to_dict方法的具体用法?Python Post.to_dict怎么用?Python Post.to_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Post
的用法示例。
在下文中一共展示了Post.to_dict方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: single_post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import to_dict [as 别名]
def single_post(post_id):
"""Returns everything in a post as json"""
post_d = mongo.db.posts.find_one(post_id)
if post_d is None:
abort(404)
post = Post(bson=post_d)
return jsonify(**post.to_dict())
示例2: home
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import to_dict [as 别名]
def home():
page_num = int(request.args.get('page_num', 1))
limit = 30
offset = limit * (page_num-1)
posts = [Post.to_dict(post) for post in Post.get_posts(viewer_id=g.loggedin_user.id, offset=offset, limit=limit)]
more_posts = True
if len(posts)<limit:
more_posts = False
return render_template('post_list.html', post_list=posts, page_num=page_num, more_posts=more_posts)
示例3: create_post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import to_dict [as 别名]
def create_post():
"""Creates a new post with a new id"""
if not request.json:
abort(400)
form = PostEditForm.from_json(request.json, skip_unkown_keys=False)
if form.validate():
post = Post(json=form.data)
objId = mongo.db.posts.insert(post.to_bson())
post.id = str(objId)
return jsonify(**post.to_dict()), 201
else:
abort(400)
示例4: edit_post
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import to_dict [as 别名]
def edit_post(post_id):
"""Replaces post behind id with submitted one"""
post_d = mongo.db.posts.find_one(post_id)
if post_d is None:
abort(404)
form = PostEditForm.from_json(request.json, skip_unknown_keys=False)
if form.validate():
post = Post(bson=post_d)
post.update(form.data)
query = {'_id': post_id}
mongo.db.posts.update(query, post.to_bson())
# needed for chaining multiple edits together
token = generate_csrf_token()
return jsonify(_csrf=token, **post.to_dict())
else:
print form.errors #TODO
abort(400)
示例5: add
# 需要导入模块: from models import Post [as 别名]
# 或者: from models.Post import to_dict [as 别名]
def add(title, content):
post = Post(title=title, content=content)
db.add(post)
db.commit()
return post.to_dict()