diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2015-05-20 23:24:45 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2015-05-20 23:24:45 +0200 |
commit | fa6d60d17ddb7b70c6bc2337d901ef8cc924e67b (patch) | |
tree | 83fb2851ab7dd64a54732159d3099488193aac02 /factory/base.py | |
parent | 536ac1b0fe7c4a04ad144022d6394b994feccdfd (diff) | |
download | factory-boy-fa6d60d17ddb7b70c6bc2337d901ef8cc924e67b.tar factory-boy-fa6d60d17ddb7b70c6bc2337d901ef8cc924e67b.tar.gz |
Add Meta.rename to handle name conflicts (See #206).
Define ``Meta.rename = {'attrs': 'attributes'}``
if your model expects a ``attributes`` kwarg but you can't
define it since it's already reserved by the ``Factory`` class.
Diffstat (limited to 'factory/base.py')
-rw-r--r-- | factory/base.py | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/factory/base.py b/factory/base.py index d48edd5..0f2af59 100644 --- a/factory/base.py +++ b/factory/base.py @@ -176,6 +176,7 @@ class FactoryOptions(object): OptionDefault('strategy', CREATE_STRATEGY, inherit=True), OptionDefault('inline_args', (), inherit=True), OptionDefault('exclude', (), inherit=True), + OptionDefault('rename', {}, inherit=True), ] def _fill_from_meta(self, meta, base_meta): @@ -413,6 +414,12 @@ class BaseFactory(object): return decls @classmethod + def _rename_fields(cls, **kwargs): + for old_name, new_name in cls._meta.rename.items(): + kwargs[new_name] = kwargs.pop(old_name) + return kwargs + + @classmethod def _adjust_kwargs(cls, **kwargs): """Extension point for custom kwargs adjustment.""" return kwargs @@ -441,6 +448,7 @@ class BaseFactory(object): **kwargs: arguments to pass to the creation function """ model_class = cls._get_model_class() + kwargs = cls._rename_fields(**kwargs) kwargs = cls._adjust_kwargs(**kwargs) # Remove 'hidden' arguments. |