Override field widget in Django admin form

06 Mar · by Tim Kamanin · 1 min read

Approach 1

Override all widgets of a given type in a form. In this example we set CKEditor widget for all textfields in the form.

# file: admin.py
class BlogAdmin(admin.ModelAdmin):

    formfield_overrides = {
        models.TextField: {'widget': CKEditorWidget},
    }

Approach 2

Override widget only for a desired field in the form.

# file: form.py
class BlogAdminForm(ModelForm):
    class Meta:
        model = Blog
        widgets = {
            'body': CKEditorWidget(),
        }

# file: admin.py
class BlogAdmin(admin.ModelAdmin):
    form = BlogAdminForm

Comments

Required for comment verification