【发布时间】:2021-11-01 12:07:03
【问题描述】:
我有一类 Post 我希望能够键入 cmets,其值是每个评论的 to_dict() 方法的列表理解,我的问题是如何键入此列表中的每个对象以得到那个 cmets 数据?
后模型 类帖子(db.Model): 表名 = '帖子'
id = db.Column(db.Integer, primary_key=True)
caption = db.Column(db.String, nullable=False)
picture_url = db.Column(db.String(255), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
timestamp = db.Column(db.DateTime, default=datetime.now)
users = db.relationship("User", back_populates="posts")
comments = db.relationship("Comment", back_populates="posts")
postLikes = db.relationship("User", secondary=likes, back_populates="userLikes")
def to_dict(self):
user = User.query.filter(User.id == self.user_id).first()
return {
"id": self.id,
"caption": self.caption,
'picture_url': self.picture_url,
"user_id": self.user_id,
"timestamp": self.timestamp,
"user": user.to_dict(),
"post_comments": [comment.to_dict() for comment in self.comments],
"post_likes": [user.id for user in self.postLikes],
"likes_count": len(self.postLikes),
"comments_count": len(self.comments)
}
评论模型
class Comment(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key=True)
comment = db.Column(db.String(255), nullable=False)
post_id = db.Column(db.Integer, db.ForeignKey("posts.id"))
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
createdAt = db.Column(db.DateTime(timezone=True),
server_default=func.now())
updatedAt = db.Column(db.DateTime(timezone=True), onupdate=func.now())
users = db.relationship('User', back_populates='comments')
posts = db.relationship('Post', back_populates='comments')
def to_dict(self):
return {
"id": self.id,
"user": self.users.username,
"user_pic": self.users.profile_picture,
"comment": self.comment,
"post_id": self.post_id,
"createdAt": self.createdAt,
"updatedAt": self.updatedAt
}
**为每个帖子映射帖子列表:**
{post?.post_comments.map(comm => {
<div>{comm.user_pic}</div>
})}
帖子是什么样子的
{
"posts": [
{
"caption": "Hammy’s got the crazy eyes. Get ready to be pounced…",
"comments_count": 2,
"id": 1,
"likes_count": 0,
"picture_url": "https://celebritypets.net/wp-content/uploads/2017/03/Hamilton-the-Hipster-Cat-instagram.jpg",
"post_comments": [
{
"comment": "comment example",
"createdAt": "Thu, 02 Sep 2021 14:04:43 GMT",
"id": 1,
"post_id": 1,
"updatedAt": null,
"user": "hamilton_this_hipster_cat",
"user_pic": "https://i.insider.com/5654a4c0c2814477008b51d8?width=750&format=jpeg&auto=webp"
},
{
"comment": "comment on first post again",
"createdAt": "Thu, 02 Sep 2021 14:04:43 GMT",
"id": 2,
"post_id": 1,
"updatedAt": null,
"user": "DogtorLoki",
"user_pic": "https://photos.bringfido.com/posted/2020/08/04/849663/loki-1.jpg"
}
],
"post_likes": [],
"timestamp": "Wed, 01 Sep 2021 16:16:58 GMT",
"user": {
"biography": "Mustache Cat. Yes, it's real. San Francisco, CA. #AdoptDontShop",
"email": "hamilton@aa.io",
"id": 1,
"profile_picture": "https://i.insider.com/5654a4c0c2814477008b51d8?width=750&format=jpeg&auto=webp",
"username": "hamilton_this_hipster_cat"
},
"user_id": 1
},
{
"caption": "If dogs are good for your health, then two unicorns must be MAGICAL",
"comments_count": 1,
"id": 2,
"likes_count": 0,
"picture_url": "https://static.onecms.io/wp-content/uploads/sites/20/2021/01/05/dogtor-2.jpg",
"post_comments": [
{
"comment": "comment on second post",
"createdAt": "Thu, 02 Sep 2021 14:04:43 GMT",
"id": 3,
"post_id": 2,
"updatedAt": null,
"user": "hamilton_this_hipster_cat",
"user_pic": "https://i.insider.com/5654a4c0c2814477008b51d8?width=750&format=jpeg&auto=webp"
}
],
"post_likes": [],
"timestamp": "Wed, 01 Sep 2021 16:16:58 GMT",
"user": {
"biography": "A Baltimore Pup+MedStudent Mom, AMC Top Dog Honoree",
"email": "dogtorloki@aa.io",
"id": 2,
"profile_picture": "https://photos.bringfido.com/posted/2020/08/04/849663/loki-1.jpg",
"username": "DogtorLoki"
},
"user_id": 2
}
]
}
【问题讨论】:
-
使用字典理解。
标签: python reactjs dictionary model list-comprehension