matthiask / django-imagefield

You should probably use this image field instead of Django's built-in models.ImageField.
https://django-imagefield.readthedocs.io
BSD 3-Clause "New" or "Revised" License
102 stars 10 forks source link

'bool' object has no attribute 'name' when using the image field with parler TranslatableModel #9

Open underdoeg opened 3 years ago

underdoeg commented 3 years ago

I have a django cabinet custom File that inherits the TranslatableModel something like:

# models.py
class File(TranslatableModel, AbstractFile, ImageMixin):
    FILE_FIELDS = ['image_file']

    # Add caption and copyright, makes FileAdmin reuse easier.
    caption = models.CharField(
        "Interner Titel",
        max_length=512,
        blank=True,
    )

    copyright = models.CharField(
        "Copyright",
        max_length=512,
        blank=True,
    )

    translations = TranslatedFields(
        translated_image_file=ImageField(
            "Übersetztes Bild",
            auto_add_fields=True,
            upload_to=UPLOAD_TO,
            blank=True,
            max_length=1000,
        )
    )

#admin.py

@admin.register(File)
class TranslatedFileAdmin(TranslatableAdmin, FileAdmin):
    top_fields = ["folder", "caption", "translated_image_file"]

    def get_fieldsets(self, request, obj=None):
        res = super().get_fieldsets(request, obj)
        res[0][1]["fields"].append('translated_image_file')
        return res

As hacky as this is, it seems to work. Except when I try to clear the "translated_image_file".

then I get the following erro

  File "python3.9/site-packages/imagefield/fields.py", line 405, in _generate_files
    if previous and previous[0] and previous != (f.name, f._ppoi()):
AttributeError: 'bool' object has no attribute 'name'
matthiask commented 3 years ago

f should be an instance of an ImageFieldFile which – in turn – should always have a .name attribute. I'm not sure what is going on here. I do not have any experience with django-parler, maybe the problem has something to do with it?

The code which crashes is only used for clearing old thumbnails. It's nice that django-imagefield tries cleaning up after itself but if this feature proves problematic we could introduce a flag which deactivates this housekeeping. Does everything else work if you deactivate/remove https://github.com/matthiask/django-imagefield/blob/26f126f3a91b94b2e23dbc6b5da86fdb81ed0bee/imagefield/fields.py#L405-L407 ?

underdoeg commented 3 years ago

AFAIK parler dynamically creates another model that contains all the translations, and then in the admin panel automatically adds widgets for the related table entry in the current language. usually this works very well, but it does some automated stuff in the background which might cause issues. Everything else seem to work fine though. I'll do some more digging and see if I understand what is going on.

underdoeg commented 3 years ago

Looks like parler somehow loses the widget and uses the default django image widget. I cannot set the PPOI for the translated image. I feel like it behaves like a regular FileField and loses all "extra features" on clear

matthiask commented 3 years ago

That's sad. The django-imagefield widget itself is quite hacky but the activation itself through .formfield(...) isn't https://github.com/matthiask/django-imagefield/blob/26f126f3a91b94b2e23dbc6b5da86fdb81ed0bee/imagefield/fields.py#L355-L363

underdoeg commented 3 years ago

So I have no idea what exactly is happening behind the scenes but I kinda managed to work around the issue by adding this to the TranslatedFileAdmin class:

    def save_model(self, request, obj, form, change):
        if not obj.translated_image_file:
            obj.translated_image_file = None
        super().save_model(request, obj, form, change)