jeudi 30 août 2018

How to setup a sequence for an attribute while using create_batch in factory-boy?

When using factory_boy in Django, how can I achieve this?

models.py

class TestModel(models.Model):
    name = CharField()
    order = IntegerField()

recipes.py

class TestModelFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = TestModel

    name = factory.LazyAttribute(lambda o: faker.word().title())
    parent = 0

tests.py

recipes.TestModelFactory.create_batch(4, order=+10)

or

recipes.TestModelFactory.create_batch(4, order=seq(10))

or something along those lines, to achieve this result:

TestModel.objects.all().values_list('order', flat=True)

[10, 20, 30, 40]

Aucun commentaire:

Enregistrer un commentaire