mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
010ba38dc1
* Added alias_provider * Update HISTORY.rst (+ add alias_provider support #622 by @MrMrRobat) * Rename alias_provider —> alias_generator Move docs to config section, reformat example Separate test_alias_generator * Update docs/index.rst Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Update docs/index.rst Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Update docs/index.rst Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Fix conflict between dictionary field config in Config.fields and Config.alias_generator Add one more condition for applying alias_generator Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Add test for using alias_generator with field schema
22 lines
472 B
Python
22 lines
472 B
Python
from pydantic import BaseModel
|
|
|
|
def to_camel(string: str) -> str:
|
|
return ''.join(word.capitalize() for word in string.split('_'))
|
|
|
|
class Voice(BaseModel):
|
|
name: str
|
|
gender: str
|
|
language_code: str
|
|
|
|
class Config:
|
|
alias_generator = to_camel
|
|
|
|
voice = Voice(Name='Filiz', Gender='Female', LanguageCode='tr-TR')
|
|
print(voice.language_code)
|
|
print(voice.dict(by_alias=True))
|
|
|
|
"""
|
|
tr-TR
|
|
{'Name': 'Filiz', 'Gender': 'Female', 'LanguageCode': 'tr-TR'}
|
|
"""
|