summaryrefslogtreecommitdiff
path: root/factory
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polyconseil.fr>2011-08-03 18:01:26 +0200
committerRaphaël Barrois <raphael.barrois@polyconseil.fr>2011-08-03 18:01:26 +0200
commit720371777ff8cf3e5d299398d530b948034a228a (patch)
tree03328531805f69dfd6d18fd19056caced65583fc /factory
parentea86cec104f3a469318349af3ced1262d47169aa (diff)
downloadfactory-boy-720371777ff8cf3e5d299398d530b948034a228a.tar
factory-boy-720371777ff8cf3e5d299398d530b948034a228a.tar.gz
Add a 'DjangoModelFactory'.
Signed-off-by: Raphaël Barrois <raphael.barrois@polyconseil.fr>
Diffstat (limited to 'factory')
-rw-r--r--factory/base.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/factory/base.py b/factory/base.py
index 736a0e6..c474348 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -242,3 +242,20 @@ class Factory(BaseFactory):
@classmethod
def create(cls, **kwargs):
return cls._create(**cls.attributes(create=True, **kwargs))
+
+
+class DjangoModelFactory(Factory):
+ """Factory for django models.
+
+ This makes sure that the 'sequence' field of created objects is an unused id.
+
+ Possible improvement: define a new 'attribute' type, AutoField, which would
+ handle those for non-numerical primary keys.
+ """
+
+ @classmethod
+ def _setup_next_sequence(cls):
+ try:
+ return cls._associated_class.objects.values_list('id').order_by('-id')[0] + 1
+ except IndexError:
+ return 1