diff options
author | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2015-03-27 16:27:05 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2015-03-27 16:27:05 +0100 |
commit | bdc1b815cfdf3028379c6c3f18c9c47ee8298a70 (patch) | |
tree | a15ef5f62f091aa3a06d41a9a7bd40129f921aae /factory | |
parent | 03ca4ecebc914f2e120e902b8fcbe2b526460ee6 (diff) | |
download | factory-boy-bdc1b815cfdf3028379c6c3f18c9c47ee8298a70.tar factory-boy-bdc1b815cfdf3028379c6c3f18c9c47ee8298a70.tar.gz |
Respect default manager in DjangoModelFactory (Closes #192).
The previous version tries to use ``cls._default_manager`` all the time,
which breaks with ``manager.using(db_name)``.
Diffstat (limited to 'factory')
-rw-r--r-- | factory/django.py | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/factory/django.py b/factory/django.py index eb07bfb..ba81f13 100644 --- a/factory/django.py +++ b/factory/django.py @@ -45,6 +45,8 @@ from .compat import BytesIO, is_string logger = logging.getLogger('factory.generate') +DEFAULT_DB_ALIAS = 'default' # Same as django.db.DEFAULT_DB_ALIAS + def require_django(): """Simple helper to ensure Django is available.""" @@ -56,7 +58,7 @@ class DjangoOptions(base.FactoryOptions): def _build_default_options(self): return super(DjangoOptions, self)._build_default_options() + [ base.OptionDefault('django_get_or_create', (), inherit=True), - base.OptionDefault('database', 'default', inherit=True), + base.OptionDefault('database', DEFAULT_DB_ALIAS, inherit=True), ] def _get_counter_reference(self): @@ -100,12 +102,9 @@ class DjangoModelFactory(base.Factory): if model_class is None: raise base.AssociatedClassError("No model set on %s.%s.Meta" % (cls.__module__, cls.__name__)) - try: - manager = model_class._default_manager # pylint: disable=W0212 - except AttributeError: - manager = model_class.objects - - manager = manager.using(cls._meta.database) + manager = model_class.objects + if cls._meta.database != DEFAULT_DB_ALIAS: + manager = manager.using(cls._meta.database) return manager @classmethod |