Skip to content

Commit 6a8a986

Browse files
pyropytiangolo
andauthored
✨ Add possibility to override config (tiangolo#1)
* Add possibility to override config * ✅ Add tests for custom config Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
1 parent 95acfed commit 6a8a986

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

pydantic_sqlalchemy/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class OrmConfig(BaseConfig):
99
orm_mode = True
1010

1111

12-
def sqlalchemy_to_pydantic(db_model: Type) -> Type[BaseModel]:
12+
def sqlalchemy_to_pydantic(
13+
db_model: Type, *, config: Type = OrmConfig
14+
) -> Type[BaseModel]:
1315
mapper = inspect(db_model)
1416
fields = {}
1517
for attr in mapper.attrs:
@@ -23,6 +25,6 @@ def sqlalchemy_to_pydantic(db_model: Type) -> Type[BaseModel]:
2325
default = ...
2426
fields[name] = (python_type, default)
2527
pydantic_model = create_model(
26-
db_model.__name__, __config__=OrmConfig, **fields # type: ignore
28+
db_model.__name__, __config__=config, **fields # type: ignore
2729
)
2830
return pydantic_model

tests/test_pydantic_sqlalchemy.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ class Address(Base):
3232
user = relationship("User", back_populates="addresses")
3333

3434

35-
PydanticUser = sqlalchemy_to_pydantic(User)
36-
PydanticAddress = sqlalchemy_to_pydantic(Address)
37-
38-
39-
class PydanticUserWithAddresses(PydanticUser):
40-
addresses: List[PydanticAddress] = []
41-
42-
4335
Base.metadata.create_all(engine)
4436

4537

@@ -56,7 +48,13 @@ class PydanticUserWithAddresses(PydanticUser):
5648
db.commit()
5749

5850

59-
def test_pydantic_sqlalchemy():
51+
def test_defaults() -> None:
52+
PydanticUser = sqlalchemy_to_pydantic(User)
53+
PydanticAddress = sqlalchemy_to_pydantic(Address)
54+
55+
class PydanticUserWithAddresses(PydanticUser):
56+
addresses: List[PydanticAddress] = []
57+
6058
user = db.query(User).first()
6159
pydantic_user = PydanticUser.from_orm(user)
6260
data = pydantic_user.dict()
@@ -78,3 +76,35 @@ def test_pydantic_sqlalchemy():
7876
{"email_address": "eddy@example.com", "id": 2, "user_id": 1},
7977
],
8078
}
79+
80+
81+
def test_config() -> None:
82+
class Config:
83+
orm_mode = True
84+
allow_population_by_field_name = True
85+
86+
@classmethod
87+
def alias_generator(cls, string: str) -> str:
88+
pascal_case = "".join(word.capitalize() for word in string.split("_"))
89+
camel_case = pascal_case[0].lower() + pascal_case[1:]
90+
return camel_case
91+
92+
PydanticUser = sqlalchemy_to_pydantic(User)
93+
PydanticAddress = sqlalchemy_to_pydantic(Address, config=Config)
94+
95+
class PydanticUserWithAddresses(PydanticUser):
96+
addresses: List[PydanticAddress] = []
97+
98+
user = db.query(User).first()
99+
pydantic_user_with_addresses = PydanticUserWithAddresses.from_orm(user)
100+
data = pydantic_user_with_addresses.dict(by_alias=True)
101+
assert data == {
102+
"fullname": "Ed Jones",
103+
"id": 1,
104+
"name": "ed",
105+
"nickname": "edsnickname",
106+
"addresses": [
107+
{"emailAddress": "ed@example.com", "id": 1, "userId": 1},
108+
{"emailAddress": "eddy@example.com", "id": 2, "userId": 1},
109+
],
110+
}

0 commit comments

Comments
 (0)