我正在使用Parse来构建我的数据库。
我有两张桌子:Article
& Comment
。 Article
有一个或多个Comments
我正在使用Parse Resful API [ParsePy][1]
添加项目
from parse_rest.datatypes import Object
class Article(Object):
pass
class Comment(Object):
pass
articleItem = Article(title='Test', author='John Doe')
articleItem.save() # we have to save it before it can be referenced
我不知道如何实现这种1:N的关系,任何人都可以表明这样做:
答案 0 :(得分:0)
from parse_rest.datatypes import Object
class Article(Object):
pass
class Comment(Object):
pass
articleItem = Article(title='Test', author='John Doe')
# for example, you get data from json
comments = []
for data in incoming_json['comments']:
# Don't know why, but this cannot working [Comments(**data).save() for data in incoming_json['comments]]
comment = Comments(**data)
comment.save()
comments.append(comment)
articleItem.comments = comments
articleItem.save() # we have to save it before it can be referenced
如果您收到查询,您将收到:
[<Comments:v6PvbsPbQT>, <Comments:V5JpqPRuS6>, <Comments:HNocXqmUeJ>,]