Django: Change widget in admin form

0 Comments

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.

01.from django.contrib import admin
02.from django import forms
03. 
04.class SkinAdminForm(forms.ModelForm):
05.  # Modifies the image_path field so its smaller.
06.  image_path = forms.CharField()
07. 
08.  class Meta:
09.    model = Skin
10. 
11. 
12.# Admin to allow user to create skins
13.class SkinAdmin(admin.ModelAdmin):
14.  form = SkinAdminForm
15. 
16. 
17.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