Skip to content

Commit 9294463

Browse files
committed
AliasStrategy 🍩
1 parent c1bfb8b commit 9294463

File tree

6 files changed

+777
-614
lines changed

6 files changed

+777
-614
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 1.0.0-rc.10 : 02.08.2024
44

5+
- **Added**: `AliasStrategy` for overriding property name on target object during `setattr()`
6+
- **Changed**:`field_strategy` now can be also an instance of `BaseStrategy`
57
- **Fixed**: Fixed calling population methods when declared in form
68

79
## 1.0.0-rc.9 : 24.03.2023

django_api_forms/forms.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.utils.translation import gettext as _
99

1010
from .exceptions import UnsupportedMediaType, ApiFormException, DetailValidationError
11+
from .population_strategies import BaseStrategy
1112
from .settings import Settings
1213
from .utils import resolve_from_path
1314

@@ -197,7 +198,10 @@ def populate(self, obj, exclude: List[str] = None):
197198
if hasattr(self, f'populate_{key}'):
198199
self.cleaned_data[key] = getattr(self, f'populate_{key}')(obj, self.cleaned_data[key])
199200

200-
strategy()(field, obj, key, self.cleaned_data[key])
201+
if isinstance(strategy, BaseStrategy):
202+
strategy(field, obj, key, self.cleaned_data[key])
203+
else:
204+
strategy()(field, obj, key, self.cleaned_data[key])
201205

202206
return obj
203207

django_api_forms/population_strategies.py

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ def __call__(self, field, obj, key: str, value):
33
setattr(obj, key, value)
44

55

6+
class AliasStrategy(BaseStrategy):
7+
def __init__(self, property_name: str):
8+
self._property_name = property_name
9+
10+
def __call__(self, field, obj, key: str, value):
11+
setattr(obj, self._property_name, value)
12+
13+
614
class IgnoreStrategy(BaseStrategy):
715
def __call__(self, field, obj, key: str, value):
816
pass

0 commit comments

Comments
 (0)