From 91592efbdff6de4b21b3a0b1a4d86dff8818f580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Mon, 11 Mar 2013 22:07:01 +0100 Subject: Proper manager fetching in DjangoModelFactory. --- factory/base.py | 15 +++++++++++++-- 1 file 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 @@ -659,11 +659,21 @@ 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): -- cgit v1.2.3