You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/aliases.md
+16-13Lines changed: 16 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
-
# Field Aliases
1
+
# 🏷️ Field Aliases
2
2
3
3
The `--aliases` option allows you to rename fields in the generated models. This is useful when you want to use different Python field names than those defined in the schema while preserving the original names as serialization aliases.
The alias file is a JSON file that maps original field names to their Python aliases.
14
14
15
-
### Flat Format (Traditional)
15
+
### 📝 Flat Format (Traditional)
16
16
17
17
The simplest format applies aliases to all fields with the matching name, regardless of which class they belong to:
18
18
@@ -26,7 +26,7 @@ The simplest format applies aliases to all fields with the matching name, regard
26
26
27
27
This will rename all fields named `id` to `id_`, all fields named `type` to `type_`, etc.
28
28
29
-
### Scoped Format (Class-Specific)
29
+
### 🎯 Scoped Format (Class-Specific)
30
30
31
31
When you have the same field name in multiple classes but want different aliases for each, use the scoped format with `ClassName.field`:
32
32
@@ -38,14 +38,15 @@ When you have the same field name in multiple classes but want different aliases
38
38
}
39
39
```
40
40
41
-
**Priority**: Scoped aliases take priority over flat aliases. In the example above:
41
+
**⚡ Priority**: Scoped aliases take priority over flat aliases. In the example above:
42
+
42
43
-`User.name` will be renamed to `user_name`
43
44
-`Address.name` will be renamed to `address_name`
44
45
- Any other class with a `name` field will use `default_name`
45
46
46
-
## Example
47
+
## 📝 Example
47
48
48
-
### Input Schema
49
+
### 📥 Input Schema
49
50
50
51
```json
51
52
{
@@ -73,7 +74,7 @@ When you have the same field name in multiple classes but want different aliases
73
74
}
74
75
```
75
76
76
-
### Alias File
77
+
### 🏷️ Alias File
77
78
78
79
```json
79
80
{
@@ -83,7 +84,7 @@ When you have the same field name in multiple classes but want different aliases
83
84
}
84
85
```
85
86
86
-
### Generated Output
87
+
### ✨ Generated Output
87
88
88
89
```python
89
90
from pydantic import BaseModel, Field
@@ -102,12 +103,14 @@ class Root(BaseModel):
102
103
address: Address |None=None
103
104
```
104
105
105
-
## Notes
106
+
## 📌 Notes
106
107
107
108
- The `ClassName` in scoped format must match the generated Python class name (after title conversion)
108
109
- When using `--use-title-as-name`, the class name is derived from the `title` property in the schema
109
110
- Aliases are applied during code generation, so the original field names are preserved as Pydantic `alias` values for proper serialization/deserialization
110
111
111
-
## See Also
112
+
---
113
+
114
+
## 📖 See Also
112
115
113
-
-[CLI Reference: `--aliases`](cli-reference/field-customization.md#aliases) - Detailed CLI option documentation with examples
0 commit comments