Django: Change widget in admin form

When you use a TextField in your model, the automatic generation for the form uses a TextArea element, which isn't always ideal.

In those instances, you will want to change the control widget so it displays as a different control type.

from django.contrib import admin
from django import forms

class SkinAdminForm(forms.ModelForm):
# Modifies the image_path field so its smaller.
image_path = forms.CharField()

class Meta:
model = Skin


# Admin to allow user to create skins
class SkinAdmin(admin.ModelAdmin):
form = SkinAdminForm


admin.site.register(Skin, SkinAdmin)

Although the "Skin" model has more than just the image_path field, changing image_path to a CharField will not affect anything else.

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog