P
P
pcdesign2015-01-27 12:00:10
Flask
pcdesign, 2015-01-27 12:00:10

How to loop through a structure obtained from Flask-SQLAlchemy?

Here is the code for querying MySQL:

U = User
I = UserItem 
items = db.session.query(U, I).join(I).filter(U.id == user_id).all()


If you print it with print(items).
Then there will be something like this:

[
(<User(id=21, nickname=Olesya, [email protected], date=2011-11-09 11:02:51)>,
  <UserItem(id=79, user_id=21, name_item=en, razg=1, revers=0, share=None)>),
 (<User(id=21, nickname=Olesya, [email protected], date=2011-11-09 11:02:51)>, 
 <UserItem(id=81, user_id=21, name_item=en, razg=1, revers=2, share=None)>)
и т.д.
 ]


How to loop through a UserItem ID (To pull all ID values ​​from the UserItem dictionary)?
How to add new keys and values ​​to UserItem?

PS: The logic is that before sending items to the template, I need to first turn
to memcached and get something from there.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ismailov, 2015-01-27
@pcdesign

for i in items:
  print i.UserItem.id

The query returns an iterator, no need to call .all():
About adding keys, explain in more detail what you mean ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question