Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6757397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:38:47+00:00 2026-05-26T13:38:47+00:00

I have some SQLAlchemy objects which contain lists of more SQLAlchemy objects, and so

  • 0

I have some SQLAlchemy objects which contain lists of more SQLAlchemy objects, and so on (for about 5 levels). I wish to convert all the objects to dictionaries.

I can convert an object to a dictionary by using the __dict__ property, no problem. However, I’m having trouble figuring out the best way to convert all the nested objects as well, without having to do each level explicitly.

So far, this is the best I can come up with, but it doesn’t recurse properly. It basically breaks after one pass, so there’s clearly something wrong with my logic. Can you see what’s wrong with it??

I am hoping to do:

all_dict = myDict(obj.__dict__)

def myDict(d):
    for k,v in d.items():
        if isinstance(v,list):
            d[k] = [myDict(i.__dict__) for i in v]
        else:
            d[k] = v
    return d
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:38:48+00:00Added an answer on May 26, 2026 at 1:38 pm

    I am not sure if I understood exactly what you want – but if I got, this function can do what you want:
    It does search recursively on an object’s attributes, yielding a nested dictionary + list structure, with the ending points being python objects not having a __dict__ attribute – which in SQLAlchemy’s case are likely to be basic Python types like numbers and strings. (If that fails, replacing the “hasattr dict” test for soemthing more sensible should fix the code for your needs.

    def my_dict(obj):
        if not  hasattr(obj,"__dict__"):
            return obj
        result = {}
        for key, val in obj.__dict__.items():
            if key.startswith("_"):
                continue
            element = []
            if isinstance(val, list):
                for item in val:
                    element.append(my_dict(item))
            else:
                element = my_dict(val)
            result[key] = element
        return result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some ASP.NET web services which all share a common helper class they
I have a Django project that uses SQLAlchemy to use some legacy ORM objects.
I have some C# / asp.net code I inherited which has a textbox which
We have some files on our website that users of our software can download.
I have an SQLAlchemy ORM class, linked to MySQL, which works great at saving
I'm using Flask framework with sqlalchemy and I can't query some related table of
Have some Perl code which is using the DBI module - (the code is
I'm using __init__() like this in some SQLAlchemy ORM classes that have many parameters
I have a database package which contains some modules namely student_table and a db
I have some UI in VB 2005 that looks great in XP Style, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.