0

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)
rgohma2
  • 1
  • 1

1 Answers1

1

Your example is REALLY inefficient.

Can you just do:

(Post
 .select()
 .join(Business)
 .join(Subscription)
 .where(Subscription.user == the_user_id))
coleifer
  • 24,887
  • 6
  • 60
  • 75
  • Thanks. I am fairly new to peewee so I was able to get it working in a different way but this is much more efficient. – rgohma2 Feb 26 '20 at 16:57