-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ORM mode: Add support for arbitrary class instances (#562)
* Support ORM objects to 'parse_obj', replace #520 * switch to GetterDict and orm_mode * tweaks * update docs * split tests and add @tiangolo's suggestion * split tests and add @tiangolo's suggestion * fix coverage
- Loading branch information
1 parent
6d5c48e
commit 3dfae21
Showing
9 changed files
with
318 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ _build/ | |
pydantic/*.c | ||
pydantic/*.so | ||
.auto-format | ||
/sandbox/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import List | ||
from sqlalchemy import Column, Integer, String | ||
from sqlalchemy.dialects.postgresql import ARRAY | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from pydantic import BaseModel, constr | ||
|
||
Base = declarative_base() | ||
|
||
class CompanyOrm(Base): | ||
__tablename__ = 'companies' | ||
id = Column(Integer, primary_key=True, nullable=False) | ||
public_key = Column(String(20), index=True, nullable=False, unique=True) | ||
name = Column(String(63), unique=True) | ||
domains = Column(ARRAY(String(255))) | ||
|
||
class CompanyModel(BaseModel): | ||
id: int | ||
public_key: constr(max_length=20) | ||
name: constr(max_length=63) | ||
domains: List[constr(max_length=255)] | ||
|
||
class Config: | ||
orm_mode = True | ||
|
||
co_orm = CompanyOrm(id=123, public_key='foobar', name='Testing', domains=['example.com', 'foobar.com']) | ||
print(co_orm) | ||
#> <__main__.CompanyOrm object at 0x7ff4bf918278> | ||
co_model = CompanyModel.from_orm(co_orm) | ||
print(co_model) | ||
#> CompanyModel id=123 public_key='foobar' name='Testing' domains=['example.com', 'foobar.com'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import List | ||
from pydantic import BaseModel | ||
|
||
class PetCls: | ||
def __init__(self, *, name: str, species: str): | ||
self.name = name | ||
self.species = species | ||
|
||
class PersonCls: | ||
def __init__(self, *, name: str, age: float = None, pets: List[PetCls]): | ||
self.name = name | ||
self.age = age | ||
self.pets = pets | ||
|
||
class Pet(BaseModel): | ||
name: str | ||
species: str | ||
|
||
class Config: | ||
orm_mode = True | ||
|
||
class Person(BaseModel): | ||
name: str | ||
age: float = None | ||
pets: List[Pet] | ||
|
||
class Config: | ||
orm_mode = True | ||
|
||
bones = PetCls(name='Bones', species='dog') | ||
orion = PetCls(name='Orion', species='cat') | ||
anna = PersonCls(name='Anna', age=20, pets=[bones, orion]) | ||
anna_model = Person.from_orm(anna) | ||
print(anna_model) | ||
#> Person name='Anna' pets=[<Pet name='Bones' species='dog'>, <Pet name='Orion' species='cat'>] age=20.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.