def add_post(cls, image_url, username): user = User.check_username(username) # 返回一个user实例 post = Post(image_url=image_url, user_id=user.id) session.add(post) session.commit() post_id = post.id session.close() return post_id
def add_post(cls, image_url, username): user = User.check_username(username) # 返回user实例 post = Post(image_url=image_url, user_id=user.id) session.add(post) session.commit() session.close() # backref="posts"原理如下 #图片实例.user===>拿到对应的user实例 #用户实例.posts===>拿到对应的posts(图片)实例
def add_post(cls, title, content, image_url, thumbnail_url, username, posttype_id): user = User.check_username(username) #返回 user实例 post = Post(title=title, content=content, image_url=image_url, thumbnail_url=thumbnail_url, user_id=user.id, posttype_id=posttype_id) session.add(post) session.commit() session.close()
def add_user(cls, username, password, **kwargs): user = User(username=username, password=password, **kwargs) session.add(user) session.commit() session.close()