问题
Subj.
Right now (Factory Boy ver. 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 save time, so if the Image
model has save
overridden and its operates with the image
, it will fail. And thats exactly my case.
So - how to make image generated before save
call?
回答1:
I've 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'
)
)
来源:https://stackoverflow.com/questions/25806428/how-to-make-factoryboys-imagefield-generate-image-before-save-is-called