[Fixed]-How to make FactoryBoy's ImageField generate image before save() is called?

25👍

I’ve found a workaround:

from django.core.files.base import ContentFile

class ImageFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Image

    image = factory.LazyAttribute(
            lambda _: ContentFile(
                factory.django.ImageField()._make_data(
                    {'width': 1024, 'height': 768}
                ), 'example.jpg'
            )
        )

Leave a comment