DjangoDjango

Override field widget in Django admin form

Mar 06, 2015 · Updated: Nov 07, 2017 · by Tim Kamanin

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.

python
# 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.

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

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

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓