简体   繁体   中英

how to use backref in peewee

basically im trying to write a index route that returns the posts of a business that a user is subscribed to the last line is throwing an error for the 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]

this is my posts model

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

Your example is REALLY inefficient.

Can you just do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM