summaryrefslogtreecommitdiff
path: root/factory/base.py
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-11 22:07:01 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-11 22:07:01 +0100
commit91592efbdff6de4b21b3a0b1a4d86dff8818f580 (patch)
treeaf98b00e9cf1706743a7097918c0a4cfd558168c /factory/base.py
parent17097d23986b43bb74421dcbb0a0f5d75433114c (diff)
downloadfactory-boy-91592efbdff6de4b21b3a0b1a4d86dff8818f580.tar
factory-boy-91592efbdff6de4b21b3a0b1a4d86dff8818f580.tar.gz
Proper manager fetching in DjangoModelFactory.
Diffstat (limited to 'factory/base.py')
-rw-r--r--factory/base.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/factory/base.py b/factory/base.py
index 44d58a7..e798c68 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -660,10 +660,20 @@ class DjangoModelFactory(Factory):
ABSTRACT_FACTORY = True
@classmethod
+ def _get_manager(cls, target_class):
+ try:
+ return target_class._default_manager
+ except AttributeError:
+ return target_class.objects
+
+ @classmethod
def _setup_next_sequence(cls):
"""Compute the next available PK, based on the 'pk' database field."""
+
+ manager = cls._get_manager(cls._associated_class)
+
try:
- return 1 + cls._associated_class._default_manager.values_list('pk', flat=True
+ return 1 + manager.values_list('pk', flat=True
).order_by('-pk')[0]
except IndexError:
return 1
@@ -671,7 +681,8 @@ class DjangoModelFactory(Factory):
@classmethod
def _create(cls, target_class, *args, **kwargs):
"""Create an instance of the model, and save it to the database."""
- return target_class._default_manager.create(*args, **kwargs)
+ manager = cls._get_manager(target_class)
+ return manager.create(*args, **kwargs)
@classmethod
def _after_postgeneration(cls, obj, create, results=None):