0

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()

1 Answer 1

1

Your user object is not an iterable type. Typically, database models are not. You need to change for user.username user.bio ... to just for user

Additionally dicts are unhashable, so you will encounter another error after fixing the first. I would recommend making your users_data an array of dicts, instead of a dict

Example:

# Ignore this, substitute with your own model
from database.models import User

users = [
    User(email="[email protected]", first="Foo", last="Bar")
]

users_data = [
    dict(
        email=user.email,
        name=f"{user.first} {user.last}",
    ) for user in users
]

An additional example converting this back to a valid JSON object

from json import dumps

from database.models import User

users = [
    User(email="[email protected]", first="Foo", last="Bar")
]

users_data = [
    dict(
        email=user.email,
        name=f"{user.first} {user.last}",
    ) for user in users
]

# This is valid JSON
rv = dumps({"users": users_data})
print(rv)

Output:

{"users": [{"email": "[email protected]", "name": "Foo Bar"}]}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, but I receive not JSON format - [<generator object users_eng.get_users.<locals>.<genexpr> at 0x000001AF38C828C8>]
Yeah, apologies, I made a mistake in the code and edited it accordingly. Try the new edited version.
AttributeError: 'list' object has no attribute 'username'
it should be - users = User.query.all()
Thanks man, maybe you know any fast solution to remove [] in the JSON?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.