How can I use different form widgets in Django admin?
I tried with something like:
class PedidoForm(forms.ModelForm):
class Meta:
model = Pedido
widgets = {
'nota': forms.Textarea(attrs={'cols': 80, 'rows': 20}),
}
But it changes in both, list view and single object view. I'd like to change开发者_StackOverflow社区 int only for the single object view. HOw can I do it?
I want this:
But not this:
Instead of overriding widgets in the Meta class, just set the widget in an override of __init__
, and specify the form in your admin class. The form you specify will only be used for the add/change views. Example:
#forms.py
from django import forms
class PedidoAdminForm(forms.ModelForm):
class Meta:
model = Pedido
def __init__(self, *args, **kwargs):
super(PedidoAdminForm, self).__init__(*args, **kwargs)
self.fields['nota'].widget = forms.Textarea()
#admin.py
from django.contrib import admin
from your_app.forms import PedidoAdminForm
from your_app.models import Pedido
class PedidoAdmin(admin.ModelAdmin):
form = PedidoAdminForm
list_editable = ['nota']
This works for me in Django 1.3. Hope that helps you out.
精彩评论