2

I currently have a model for reusable images:

class Image(models.Model):
    class Meta:
        verbose_name = _('Image')
        verbose_name_plural = _('Images')

    name = models.CharField(
        max_length=200,
        unique=False,
        blank=False,
        null=False,
        verbose_name=_('Image name')
    )
    full_size = ProcessedImageField(
        upload_to='images/',
        processors=[ResizeToFit(height=450, upscale=False)],
        format='JPEG',
        options={'quality': 90},
    )
    thumbnail = ImageSpecField(
        source='full_size',
        processors=[Thumbnail(width=200, height=115, crop=True, upscale=False)],
        format='JPEG',
        options={'quality': 60}
    )

    def __str__(self):
        return self.name

Now I'm trying to move over to the FilerImageField from django-filer.

I have added the field with a different name, here's the relevant code section:

    image = models.ForeignKey(
        Image,
        on_delete=models.SET_NULL,
        verbose_name=_('Unit image'),
        null=True,
        blank=True
    )
    img = FilerImageField(
        null=True,
        blank=True,
        related_name="unit_img",
        verbose_name=_('Unit image'),
        on_delete=models.DO_NOTHING,
    )

I have the following migration:

# Generated by Django 3.1.2 on 2020-11-02 20:09

from django.db import migrations
from filer.models import Image


def migrate_unit_images(apps, schema_editor):
    Units = apps.get_model('tageler', 'Unit')
    for unit in Units.objects.all():
        if unit.image:
            img, created = Image.objects.get_or_create(
                file=unit.image.full_size.file,
                defaults={
                    'name': unit.name,
                    'description': unit.image.name,
                }
            )
            unit.img = img
            unit.save()


class Migration(migrations.Migration):

    dependencies = [
        ('tageler', '0008_filer_create'),
    ]

    operations = [
        migrations.RunPython(migrate_unit_images),
    ]

My problem is that I get the following error:

ValueError: Cannot assign "<Image: This is a test image>": "Unit.img" must be a "Image" instance.

I thought that I just created or gotten the Image. What am I missing here? Why is my newly created Image not an "Image" instance?

Oli
  • 21
  • 2

0 Answers0