diff options
author | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2013-04-16 11:32:36 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2013-04-16 11:32:36 +0200 |
commit | ef5011d7d28cc7034e07dc75a6661a0253c0fe1d (patch) | |
tree | 740e892080ac6faef32b8ab9c1c6792a4185cc17 /factory | |
parent | 68b5872e8cbd33f5f59ea8d859e326eb0ff0c6eb (diff) | |
download | factory-boy-ef5011d7d28cc7034e07dc75a6661a0253c0fe1d.tar factory-boy-ef5011d7d28cc7034e07dc75a6661a0253c0fe1d.tar.gz |
Don't use objects.get_or_create() unless required.
Diffstat (limited to 'factory')
-rw-r--r-- | factory/base.py | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/factory/base.py b/factory/base.py index 0d03838..2ff2944 100644 --- a/factory/base.py +++ b/factory/base.py @@ -587,8 +587,8 @@ class DjangoModelFactory(Factory): return 1 @classmethod - def _create(cls, target_class, *args, **kwargs): - """Create an instance of the model, and save it to the database.""" + def _get_or_create(cls, target_class, *args, **kwargs): + """Create an instance of the model through objects.get_or_create.""" manager = cls._get_manager(target_class) assert 'defaults' not in cls.FACTORY_DJANGO_GET_OR_CREATE, ( @@ -596,18 +596,25 @@ class DjangoModelFactory(Factory): "(in %s.FACTORY_DJANGO_GET_OR_CREATE=%r)" % (cls, cls.FACTORY_DJANGO_GET_OR_CREATE)) - if cls.FACTORY_DJANGO_GET_OR_CREATE: - key_fields = {} - for field in cls.FACTORY_DJANGO_GET_OR_CREATE: - key_fields[field] = kwargs.pop(field) - key_fields['defaults'] = kwargs - else: - key_fields = kwargs + key_fields = {} + for field in cls.FACTORY_DJANGO_GET_OR_CREATE: + key_fields[field] = kwargs.pop(field) + key_fields['defaults'] = kwargs obj, _created = manager.get_or_create(*args, **key_fields) return obj @classmethod + def _create(cls, target_class, *args, **kwargs): + """Create an instance of the model, and save it to the database.""" + manager = cls._get_manager(target_class) + + if cls.FACTORY_DJANGO_GET_OR_CREATE: + return cls._get_or_create(target_class, *args, **kwargs) + + return manager.create(*args, **kwargs) + + @classmethod def _after_postgeneration(cls, obj, create, results=None): """Save again the instance if creating and at least one hook ran.""" if create and results: |