codingjoe/django-stdimage

render_variations() call not working in Migrations

Closed this issue · 4 comments

chgad commented

Hi,

i'm switching to the StdImage from a simple ImageFiled.

I'm calling instance.img.render_variations() during the data migration but the variations are missing afterwards.

However, if i enter the django shell, fetch the same instance and execute instance.img.render_variations() all variations are created.

What is going wrong here (except the obvious)?

chgad commented

After a lot of try and Error i found out that the fieldatrribute of the StdImageField hasn't set the variaitons during Migrations.

To clarify my Initial scenario and my findings:

Suppose you start out with a pure FileField or ImageField and you want to switch to the StdImageField.
Unless you want to execute the CLI rendervariations comand you might consider creating the variations inside the migration.
This does not work by simply calling YourModelInstance.your_img_filed.render_variations().

Reason: The YourModelInstance.your_img_filed.field wasn't provided the variationsdefined in your_img_field.
FIx: Just reassign the correct field like so

YourModelInstance.your_img_filed.field = MediaObjectField(variations={
        "large": (1200, None),
        "medium": (900, None),
        "small": (600, None),
        "thumbnail": (100, 100, True)})

and call render_variations() afterwards.

Nevertehless i have still no idea why this field gets set up incorrectly in migrations but is all fine in the django shell.

Hi @chgad,

Thank you for reaching out. I am planing a new WebP and AVIF field as a new feature for this package. I will use the opportunity to check how migrations work. I believe the expected behavior would be, that the migrations are created automatically, when you add the field or a change the variations' config, right?

Best,
Joe

chgad commented

Yes, that's waht i would expect.

A small addition:
We also ahve a "Meta" Modell for our Images for which we are switchingover to StdImageField and inside the Data migration we create new Model-Instances "by hand" from old, already existing images. In this case also no variations are created automatically and the above hack has to be apllied.

Hi @chgad, I've been very busy working on a successor to this package, based on modern web standards. I am currently working on the migration feature: codingjoe/django-pictures#3
Maybe give it a spin? It's still in alpha, but with your help, we can get a stable release sooner.

I will close the issue here for now.