Changing default form Field instance for given database Field in Django admin

Content on this page is probably outdated and represents my personal knowledge, feelings and understading of things at that time.

First let me try to sum up the reason why I wanted to change the default form Field for given database Field.
In one of my web pages, I have a model which consists of multiple TextFields, and on some of them I want to use TinyMCE, but not on all of them. So I needed to add a class to the textarea, so TinyMCE doesen’t manipulate that textarea.
So to add a class to the textarea, I needed to tweak/change the widget of this particular database Fields instance. To achive this I overrided formfield_for_dbfield method in my ModelAdmin like this:


def formfield_for_dbfield(self, db_field, **kwargs):
"""
Hook for specifying the form Field instance for a given database Field
instance.
"""
if db_field.name == ‘head’:
kwargs[‘widget’] = Textarea(attrs={‘class’:‘mceNoEditor’})
return db_field.formfield(**kwargs)
else:
return super(PageAdmin, self).formfield_for_dbfield(db_field, **kwargs)

This code check if the db_field’s name attribute is head, if it is - it returns a twealed form Field instance, else it calls the parent ( original ) method.

Note: Works on Django 1.0.x, haven’t tested it in 1.1