[英]how to use backref in peewee
基本上我正在尝试编写一个索引路由,该路由返回用户订阅的最后一行的业务帖子正在为 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)
你的例子是非常低效的。
你能不能这样做:
(Post
.select()
.join(Business)
.join(Subscription)
.where(Subscription.user == the_user_id))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.