Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you Deserialize Nested Data? #92

Open
mikey-no opened this issue Sep 30, 2020 · 2 comments
Open

How do you Deserialize Nested Data? #92

mikey-no opened this issue Sep 30, 2020 · 2 comments
Assignees
Labels
question Further information is requested
Milestone

Comments

@mikey-no
Copy link

Assuming this is a valid question. How do I load one dictionary nested in another?

i.e.

    in_p1 = { "id": 1, "name": "Parent 1", "children": { "id": 1, "name": "Child 2" }}
    p1 = Parent.new_from_dict(in_p1);
    pprint(p1, indent=3)

Both Parent and Child models are defined and work individually but not together. The relationship between children and parents are also defined and this too works but not with any of the create_new_instance methods.

Have I missed something?

Thanks

Mike

@insightindustry insightindustry self-assigned this Oct 12, 2020
@insightindustry insightindustry added the question Further information is requested label Oct 12, 2020
@insightindustry
Copy link
Owner

Hi @mikey-no : Happy to help explain!

First, I'd just like to make sure I'm understanding the question correctly. Is the following correct:

  1. You have one model Parent.
  2. You have another model Child.
  3. Parent has one attribute children. Each Parent instance may have multiple Child instances in a 1:many relationship.
  4. When instantiating a new Parent instance, you want to be able to supply dict (or other serialized forms) of underlying Child instances as children.

Do I understand that correctly?

@mikey-no
Copy link
Author

Thanks very much for your response. I think I have answered my own question. I would not win any points for style.

Firstly, your questions:

  1. One Parent model
  2. Many Child model(s)
  3. Each parent may have many children.

When I supply a Parent with many children I want the parent and the new children to be serialized.... Thought I tried this before I raised my question. But here is my own answer:

def input_dict_test_with_warning_off(session):
    in_c1 = {"name": "Child 1"}
    in_c2 = {"name": "Child 2"}
    in_p1 = {"name": "Parent 1",
             "children": [
                 {"name": "Child 3"},
                 {"name": "Child 4"},
                 ]
             }

    c1 = Child.new_from_dict(in_c1);
    c2 = Child.new_from_dict(in_c2);
    p1 = Parent.new_from_dict(in_p1);
    session.add_all([c1,c2,p1])
    session.commit()

    child_list = session.query(Child).all()

    warnings.warn("ignore",MaximumNestingExceededWarning)

    for child in child_list:
        print(child.to_json())

    warnings.resetwarnings()

    
if __name__ == '__main__':
    session = create_orm()

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        input_dict_test_with_warning_off(session)
        warnings.resetwarnings()

In retrospect it looks quite simple. I don't like much having to suppress the warnings. Using max_nesting seamed to have no effect.

I'd be grateful for suggestions.

Thanks again

Mike

@insightindustry insightindustry added this to the 0.7.1 milestone Jan 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants