Snippet: Override field widget in Django admin form

Approach 1. Override all widgets of a given type in a form. In this example we set CKEditor widget for all textfields in a form.
1
2
3
4
5
6
  1. # file: admin.py
  2. class BlogAdmin(admin.ModelAdmin):
  3. formfield_overrides = {
  4. models.TextField: {'widget': CKEditorWidget},
  5. }
Approach 2. Override widget only for a desired field in a form.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  1. # file: form.py
  2. class BlogAdminForm(ModelForm):
  3. class Meta:
  4. model = Blog
  5. widgets = {
  6. 'body': CKEditorWidget(),
  7. }
  8. # file: admin.py
  9. class BlogAdmin(admin.ModelAdmin):
  10. form = BlogAdminForm