【问题标题】:how to use backref in peewee如何在 peewee 中使用 backref
【发布时间】:2020-06-09 17:12:48
【问题描述】:

基本上我试图编写一个索引路由来返回用户订阅的业务的帖子 最后一行为 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)

【问题讨论】:

  • 不知道为什么这个问题提到了 backref。

标签: python backend flask-peewee


【解决方案1】:

您的示例确实效率低下。

你能做到吗:

(Post
 .select()
 .join(Business)
 .join(Subscription)
 .where(Subscription.user == the_user_id))

【讨论】:

  • 谢谢。我对 peewee 还很陌生,所以我能够让它以不同的方式工作,但这样效率更高。
猜你喜欢
  • 2021-06-29
  • 1970-01-01
  • 2012-06-01
  • 2013-12-02
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 2017-11-23
相关资源
最近更新 更多