Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change alias precedence to prefer child models (#904)
* Change alias precedence to prefer child models * add docs about alias precedence * correct change
- Loading branch information
1 parent
87fb5cc
commit ed7b216
Showing
7 changed files
with
81 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**Breaking Change:** Change the precedence of aliases so child model aliases override parent aliases, | ||
including using `alias_generator` |
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,21 @@ | ||
from pydantic import BaseModel | ||
|
||
class Voice(BaseModel): | ||
name: str | ||
language_code: str | ||
|
||
class Config: | ||
@classmethod | ||
def alias_generator(cls, string: str) -> str: | ||
# this is the same as `alias_generator = to_camel` above | ||
return ''.join(word.capitalize() for word in string.split('_')) | ||
|
||
class Character(Voice): | ||
mood: str | ||
|
||
class Config: | ||
fields = {'mood': 'Mood', 'language_code': 'lang'} | ||
|
||
c = Character(Mood='happy', Name='Filiz', lang='tr-TR') | ||
print(c) | ||
print(c.dict(by_alias=True)) |
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