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

  • SEARCH
  • Home
  • 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 6544147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:25:52+00:00 2026-05-25T11:25:52+00:00

Ok, so I’m having a bit of a problem here. I need to be

  • 0

Ok, so I’m having a bit of a problem here. I need to be able to create a sort of import/export functionality for some sqlalchemy. Now these are not objects I’m defining, so to get the columns I’m doing:

for attr, value in res.__class__.__dict__.iteritems():
     if isinstance(value, InstrumentedAttribute):
          data = eval("res." + str(attr))
          param_dict[attr] = data

Now this correctly gets me the attributes of that object. However, I can;t be certain that the parameters of the init are the same, since I’m not the one handling this objects. So there could be a situation like:

class SomeClass(model.Base):
    my_column = Column(String)
    ....some other stuff...

    def __init__(self, mycolumn, ...):
           self.my_column = mycolumn

So in this case I don’t have any correspondance between the name of the field and the name of the parameter as recieved by init. I’m currently contraining the ones who define these classes to have all the init parametes with a default value so I could just:

obj = SomeClass()
exec "obj." + attr + " = " + param[attr]

However I would like to get away even from this constrain. Is there any way I can achieve this?

  • 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-25T11:25:53+00:00Added an answer on May 25, 2026 at 11:25 am

    Serializing can’t really be generalized for all possible sqlalchemy mapped classes, classes might have properties that aren’t stored in the database, or that must be inferred across multiple levels of indirection through relationship properties. In short, only you know how to serialize a particular class for a particular use.


    Lets pretend that you only need or care about the column values for the specific instance of the specific class under consideration (in res). Here’s a crude function that will return a dict containing only those values.

    from sqlalchemy.orm.attributes import manager_of_class
    from sqlalchemy.orm.properties import ColumnProperty
    
    def get_state_dict(instance):
        cls = type(instance)
        mgr = manager_of_class(cls)
        return dict((key, getattr(instance, key))
                    for key, attr in mgr.iteritems()
                    if isinstance(attr.property, ColumnProperty))
    

    and to recreate an instance from the dict*

    def create_from_state_dict(cls, state_dict):
        mgr = manager_of_class(cls)
        instance = mgr.new_instance()
        for key, value in state_dict.iteritems():
            setattr(instance, key, value)
        return instance
    

    If you need something more complex, probably handling the relationships not shown as columns (as in many-to-many relationships), you can probably add that case by looking for sqlalchemy.orm.properties.RelationshipProperty, and then iterating over the collection.

    *Serializing the intermediate dict and class is left as an exercise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.