Issue
I have this table with multiple table relationships
class _FoodsInStock(Base):
__tablename__ = 'FoodsInStock'
date = Column(String, primary_key=True)
breakfasts = relationship('_BreakfastsInStock')
lunch = relationship('_LunchInStock')
snacks = relationship('_SnacksInStock')
cereals = relationship('_CerealsInStock')
fruits = relationship('_FruitsInStock')
cookies = relationship('_CookiesInStock')
chocolates = relationship('_ChocolatesInStock')
others = relationship('_OtherFoodsInStock')
I am trying to get a specific table that is related to the _FoodsInStock
table
I know I can do something as simple as this:
table = _FoodInStock.query.get(id).breakfasts
That works perfectly but I need to get the table related depending on some input, and doing that would take me to write that line per line
Is there any way of doing something to have the tables related depending of an input? something like in a dictionary:
tables = ['breakfasts', 'lunch', 'snacks', 'cereals', 'fruits', 'cookies', 'chocolates', 'others']
for table in tables:
q = _FoodInStock.query.get(id)[table]
Thank you!
Solution
As these are attributes, you should be able to achieve this with getattr
:
tables = ['breakfasts', 'lunch', 'snacks', 'cereals', 'fruits', 'cookies', 'chocolates', 'others']
for table in tables:
q = getattr(_FoodInStock.query.get(id), table)
Answered By - match
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.