Django — Admin Inline Styling / Widget Attrs / Overriding Widgets

Quickly style all of your inlines with formfield_for_dbfield

class PersonInline(admin.TabularInline):
	model = Person
	extra = 6
	def formfield_for_dbfield(self, db_field, **kwargs):
		attrs = { 'size': 15 }
		if db_field.attname == 'interest_level':
			attrs = { 'size': 2 }
		kwargs['widget'] = forms.TextInput(attrs=attrs)
		return super(PersonInline, self).formfield_for_dbfield(db_field,**kwargs)

I see this used for things like custom widgets, ReadOnlyWidget, ColoredWidget, etc, but I wanted a quick and dirty(?) way to fit more fields on my inlines, since the admin just overflows.

3 thoughts on “Django — Admin Inline Styling / Widget Attrs / Overriding Widgets

  1. That means you don’t have forms defined, just as if you started up python and wrote some variable that doesn’t exist.

    you need to import forms:
    from django import forms

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