How do I make FactoryBoy ImageField generate an image before calling save ()?
Subj.
Right now (Factory Boy version 2.4.1.) With this code:
class ImageFactory(factory.django.DjangoModelFactory):
class Meta:
model = Image
image = factory.django.ImageField(width=1024, height=768)
image
will be None
at the time of saving, so if the model image
has been save
overridden and its function works with image
, it will not work. And this is definitely my business.
So - how to make the image generated before the call save
?
+3
Gill bates
source
to share
1 answer
I found a workaround:
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'
)
)
+8
Gill bates
source
to share