Search code examples
djangodjango-admindjango-modelsdjango-modeladmin

How to customize the "Clear" checkbox for a models.ImageField in django Admin page?


i have a Model for a user profile in my django app that has a models.ImageField and i have an ModelAdmin for it when a user uploads an image , in the admin page , when i go in that user's Customize page , in the ImageField section , there is the url of uploaded image and a checkbox named "Clear" and a button for updating the image. how can i change the text of that checkbox ? for example i want it to have the text "Delete" instead of "Clear"


Solution

  • It seems like "Clear" is hardcode.

    So either you create a custom widget simply like that:

    class MyClearableFileInput(ClearableFileInput):
        clear_checkbox_label = ugettext_lazy('Delete')
    

    And assign it to your form field like that

    MyForm(forms.Form):
        myfile=ImageField(widget=MyClearableFileInput)
    

    Or add overwrite it in your admin

    class MyModelAdmin(admin.ModelAdmin):
        formfield_overrides = {
            models.ImageField: {'widget': MyClearableFileInput},
        }
    

    Or you use the translation mechanisms to translate Clear into Delete. Django translation is described in the docs pretty well.

    I personally, just think that it is quite some overhead for your problem, unless you are using translations anyway. I would clearly recommend the custom widget - the addtional code is really minimal.