Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image field admin deletion #311

Open
JKotras opened this issue Mar 14, 2022 · 1 comment
Open

Image field admin deletion #311

JKotras opened this issue Mar 14, 2022 · 1 comment

Comments

@JKotras
Copy link

JKotras commented Mar 14, 2022

When you save translated model with ImageFiled without an image, the Parler saves the value "False" into a database.

Translated model with image field:

class Banner(BaseModel, TranslatableModel):
    name = models.CharField(max_length=50, null=False, blank=False, help_text='General banner administration name')
    active = models.BooleanField(default=False)
    title = TranslatedField()
    image = TranslatedField()

    def __unicode__(self):
        return self.title


class BannerTranslation(TranslatedFieldsModel):
    master = models.ForeignKey(Banner, related_name='translations', on_delete=models.CASCADE, null=True)
    title = models.CharField(max_length=200, null=True, blank=True)
    image = ImageField(null=True, blank=True, default=None)

Administration:

@admin.register(Banner)
class BannerAdmin(TranslatableAdmin):
    list_display = ('id', 'name', 'active')
    list_filter = ('active',)
    list_display_links = ('id', 'name',)
    search_fields = ('id', 'name',)

    fieldsets = (
        (None, {
            'fields': ('name', 'active'),
        }),
        ('Banner Content', {
            'fields': ('title', 'image'),
        }),
    )

When you save the administration form without an image (or you check "remove") the value "False" is saved into a database. Finally the Django is looking for "False" file.
1d23b0e2-8238-4288-ba26-d70d0899f296

@JKotras
Copy link
Author

JKotras commented Mar 14, 2022

My temporal solution is:

class BannerTranslatableModelForm(TranslatableModelForm):

    def clean_image(self):
        data = self.cleaned_data['image']
        if not data:
            return ''
        return data

@admin.register(Banner)
class BannerAdmin(TranslatableAdmin):
    list_display = ('id', 'name', 'active')
    list_filter = ('active', )
    list_display_links = ('id', 'name',)
    search_fields = ('id', 'name',)
    form = BannerTranslatableModelForm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant