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

℡╲_俬逩灬. 提交于 2020-03-01 01:59:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!