0

基本上我试图编写一个索引路由来返回用户订阅的企业的帖子最后一行是为 backref (business.posts) 抛出错误

# query that finds all the subscriptions of the logged in user
subscriptions_query = models.Subscription.select().join(models.User).where(models.User.id == current_user.id)
# gets the businesses being followed from the subscriptions
businesses_being_followed = [subscription.following for subscription in subscriptions_query]
post_dicts = [model_to_dict(business.posts) for business in businesses_being_followed]

这是我的帖子模型

class Post(BaseModel):
business = ForeignKeyField(Business, backref='posts')
image = CharField()
content = CharField()
date = DateTimeField(default=datetime.datetime.now)
4

1 回答 1

1

您的示例确实效率低下。

你能做到吗:

(Post
 .select()
 .join(Business)
 .join(Subscription)
 .where(Subscription.user == the_user_id))
于 2020-02-26T02:56:53.930 回答