I'm trying to make a full JSON object return from the module, which gets all users from database and enters their data in one JSON object, but I get TypeError: cannot unpack non-iterable User object, I don't really know what's the problem.
from application.models import User
class users_eng:
def __init__(self):
pass
def get_users(self):
users = User.query.all()
# users output - [<User 9nematix>, <User 3nematix>, <User 6nematix>]
for user in users:
users_data = {
dict(
username=user.username,
bio=user.profile_bio,
role=user.role,
profile_picture=user.profile_picture
) for user.username, user.profile_bio, user.role, user.profile_picture, *_ in users
}
x = json.dumps(users_data)
print(x)
return x
_users_ = users_eng()
** EDIT: I updated my code with the other solution in comments, but now I get [<generator object users_eng.get_users.<locals>.<genexpr> at 0x000001AF38C828C8>]
from application.models import User
from flask import session
import json
class users_eng:
def __init__(self):
pass
def get_users(self):
users = [
User.query.all()
]
# users output - [<User 9nematix>, <User 3nematix>, <User 6nematix>]
users_data = []
for user in users:
users_data.append(
dict(
username=user.username,
bio=user.profile_bio,
role=user.role,
profile_picture=user.profile_picture,
) for user in users
)
print(users_data)
return users_data
_users_ = users_eng()