summaryrefslogtreecommitdiff
path: root/factory/base.py
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-11-25 00:51:31 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-11-25 00:51:31 +0100
commitccd6a9927483b8789dee13c2d68a4407e2d37f94 (patch)
treef4824ea76bdf370f71be124dad2f61b32f666728 /factory/base.py
parent689b06c59788dedfce0af444760a3e5966761016 (diff)
downloadfactory-boy-ccd6a9927483b8789dee13c2d68a4407e2d37f94.tar
factory-boy-ccd6a9927483b8789dee13c2d68a4407e2d37f94.tar.gz
django: Fix lazy loading of 'son' factories (Closes #109).
Diffstat (limited to 'factory/base.py')
-rw-r--r--factory/base.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/factory/base.py b/factory/base.py
index 8183649..3c6571c 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -175,11 +175,13 @@ class FactoryMetaClass(type):
is_abstract = attrs.pop('ABSTRACT_FACTORY', False)
base = parent_factories[0]
- inherited_associated_class = getattr(base,
- CLASS_ATTRIBUTE_ASSOCIATED_CLASS, None)
+ inherited_associated_class = base._get_target_class()
associated_class = mcs._discover_associated_class(class_name, attrs,
inherited_associated_class)
+ # Invoke 'lazy-loading' hooks.
+ associated_class = base._load_target_class(associated_class)
+
if associated_class is None:
is_abstract = True
@@ -379,13 +381,19 @@ class BaseFactory(object):
return kwargs
@classmethod
- def _load_target_class(cls):
+ def _load_target_class(cls, class_definition):
"""Extension point for loading target classes.
This can be overridden in framework-specific subclasses to hook into
existing model repositories, for instance.
"""
- return getattr(cls, CLASS_ATTRIBUTE_ASSOCIATED_CLASS)
+ return class_definition
+
+ @classmethod
+ def _get_target_class(cls):
+ """Retrieve the actual, associated target class."""
+ definition = getattr(cls, CLASS_ATTRIBUTE_ASSOCIATED_CLASS, None)
+ return cls._load_target_class(definition)
@classmethod
def _prepare(cls, create, **kwargs):
@@ -395,7 +403,7 @@ class BaseFactory(object):
create: bool, whether to create or to build the object
**kwargs: arguments to pass to the creation function
"""
- target_class = cls._load_target_class()
+ target_class = cls._get_target_class()
kwargs = cls._adjust_kwargs(**kwargs)
# Remove 'hidden' arguments.