03
Sep
11

django override widget to be used by field



I wanted to override the widget used by one of my fields, in the admin. Here’s how I ended up doing:



class MyAdmin(admin.ModelAdmin):
  def formfield_for_dbfield(self,db_field,**kwargs):
     field = super(MyAdmin,self).formfield_for_dbfield(db_field,**kwargs)
     if db_field.name == "name_of_field_i_want_to_override":
        field.widget = MyCustomWidget
     return field

Advertisements

0 Responses to “django override widget to be used by field”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 223,857 hits

%d bloggers like this: