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

Serialize models with __root__ #730

Closed
Pasha19 opened this issue Aug 9, 2019 · 4 comments
Closed

Serialize models with __root__ #730

Pasha19 opened this issue Aug 9, 2019 · 4 comments
Labels
Change Suggested alteration to pydantic, not a new feature nor a bug Feedback Wanted question
Milestone

Comments

@Pasha19
Copy link

Pasha19 commented Aug 9, 2019

Question

  • Python version import sys; print(sys.version): 3.6
  • Pydantic version import pydantic; print(pydantic.VERSION): 0.30

How to serrialize models with root

from pydantic import BaseModel
from typing import List

class Pets(BaseModel):
    __root__: List[str]

pets = Pets(__root__=['dog', 'cat'])
print(pets.json())

result: {"__root__": ["dog", "cat"]}
How can I get ["dog","cat"]?

@samuelcolvin
Copy link
Member

pets.__root__

@samuelcolvin
Copy link
Member

Just for future reference "["dog","cat"]" is ambiguous, it could be a python object or a JSON string without the quotes, best to be clear what you're taking about.

I guess json() should be changed as per v1 to serialize m.__root__ if it's defined that would fundamentally change it's behaviour from dict() but I think makes sense.

Anyone else, thoughts?

@samuelcolvin samuelcolvin added Change Suggested alteration to pydantic, not a new feature nor a bug Feedback Wanted labels Aug 10, 2019
@samuelcolvin samuelcolvin added this to the Version 1 milestone Aug 10, 2019
@samuelcolvin samuelcolvin reopened this Aug 14, 2019
@tommyjcarpenter
Copy link

what is the pydantic 2 version of this question? because root is gone

alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
Co-authored-by: Oli Parker <oliver.parker@gridedge.co.uk>
Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
@will-e-yams
Copy link

will-e-yams commented Feb 18, 2024

@tommyjcarpenter , the v2 property is just .root instead of .__root__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Change Suggested alteration to pydantic, not a new feature nor a bug Feedback Wanted question
Projects
None yet
Development

No branches or pull requests

4 participants