|
3 | 3 | # py-automapper |
4 | 4 |
|
5 | 5 | **Version** |
6 | | -1.0.3 |
| 6 | +1.0.4 |
7 | 7 |
|
8 | 8 | **Author** |
9 | 9 | anikolaienko |
@@ -33,6 +33,9 @@ Table of Contents: |
33 | 33 | - [Different field names](#different-field-names) |
34 | 34 | - [Overwrite field value in mapping](#overwrite-field-value-in-mapping) |
35 | 35 | - [Extensions](#extensions) |
| 36 | + - [Pydantic/FastAPI Support](#pydanticfastapi-support) |
| 37 | + - [TortoiseORM Support](#tortoiseorm-support) |
| 38 | + - [Create your own extension (Advanced)](#create-your-own-extension-advanced) |
36 | 39 |
|
37 | 40 | # Versions |
38 | 41 | Check [CHANGELOG.md](/CHANGELOG.md) |
@@ -120,14 +123,81 @@ print(vars(public_user_info)) |
120 | 123 | # {'full_name': 'John Cusack', 'profession': 'engineer'} |
121 | 124 | ``` |
122 | 125 |
|
| 126 | + |
123 | 127 | ## Extensions |
124 | | -`py-automapper` has few predefined extensions for mapping to classes for frameworks: |
| 128 | +`py-automapper` has few predefined extensions for mapping support to classes for frameworks: |
125 | 129 | * [FastAPI](https://github.com/tiangolo/fastapi) and [Pydantic](https://github.com/samuelcolvin/pydantic) |
126 | 130 | * [TortoiseORM](https://github.com/tortoise/tortoise-orm) |
127 | 131 |
|
| 132 | +## Pydantic/FastAPI Support |
| 133 | +Out of the box Pydantic models support: |
| 134 | +```python |
| 135 | +from pydantic import BaseModel |
| 136 | +from typing import List |
| 137 | +from automapper import mapper |
| 138 | + |
| 139 | +class UserInfo(BaseModel): |
| 140 | + id: int |
| 141 | + full_name: str |
| 142 | + public_name: str |
| 143 | + hobbies: List[str] |
| 144 | + |
| 145 | +class PublicUserInfo(BaseModel): |
| 146 | + id: int |
| 147 | + public_name: str |
| 148 | + hobbies: List[str] |
| 149 | + |
| 150 | +obj = UserInfo( |
| 151 | + id=2, |
| 152 | + full_name="Danny DeVito", |
| 153 | + public_name="dannyd", |
| 154 | + hobbies=["acting", "comedy", "swimming"] |
| 155 | +) |
| 156 | + |
| 157 | +result = default_mapper.to(PublicUserInfo).map(obj) |
| 158 | +# same behaviour with preregistered mapping |
| 159 | + |
| 160 | +print(vars(result)) |
| 161 | +# {'id': 2, 'public_name': 'dannyd', 'hobbies': ['acting', 'comedy', 'swimming']} |
| 162 | +``` |
| 163 | + |
| 164 | +## TortoiseORM Support |
| 165 | +Out of the box TortoiseORM models support: |
| 166 | +```python |
| 167 | +from tortoise import Model, fields |
| 168 | +from automapper import mapper |
| 169 | + |
| 170 | +class UserInfo(Model): |
| 171 | + id = fields.IntField(pk=True) |
| 172 | + full_name = fields.TextField() |
| 173 | + public_name = fields.TextField() |
| 174 | + hobbies = fields.JSONField() |
| 175 | + |
| 176 | +class PublicUserInfo(Model): |
| 177 | + id = fields.IntField(pk=True) |
| 178 | + public_name = fields.TextField() |
| 179 | + hobbies = fields.JSONField() |
| 180 | + |
| 181 | +obj = UserInfo( |
| 182 | + id=2, |
| 183 | + full_name="Danny DeVito", |
| 184 | + public_name="dannyd", |
| 185 | + hobbies=["acting", "comedy", "swimming"], |
| 186 | + using_db=True |
| 187 | +) |
| 188 | + |
| 189 | +result = default_mapper.to(PublicUserInfo).map(obj) |
| 190 | +# same behaviour with preregistered mapping |
| 191 | + |
| 192 | +# filtering out protected fields that start with underscore "_..." |
| 193 | +print({key: value for key, value in vars(result) if not key.startswith("_")}) |
| 194 | +# {'id': 2, 'public_name': 'dannyd', 'hobbies': ['acting', 'comedy', 'swimming']} |
| 195 | +``` |
| 196 | + |
| 197 | +## Create your own extension (Advanced) |
128 | 198 | When you first time import `mapper` from `automapper` it checks default extensions and if modules are found for these extensions, then they will be automatically loaded for default `mapper` object. |
129 | 199 |
|
130 | | -What does extension do? To know what fields in Target class are available for mapping `py-automapper` need to extract the list of these fields. There is no generic way to do that for all Python objects. For this purpose `py-automapper` uses extensions. |
| 200 | +**What does extension do?** To know what fields in Target class are available for mapping, `py-automapper` needs to know how to extract the list of fields. There is no generic way to do that for all Python objects. For this purpose `py-automapper` uses extensions. |
131 | 201 |
|
132 | 202 | List of default extensions can be found in [/automapper/extensions](/automapper/extensions) folder. You can take a look how it's done for a class with `__init__` method or for Pydantic or TortoiseORM models. |
133 | 203 |
|
|
0 commit comments