How can I use various 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 both in the list view and in the view of one object. I would like to change int only to represent one object. HOw can I do this?

I want it:

enter image description here

But not this:

enter image description here

+3
source share
1 answer

Instead of overriding widgets in the Meta class, simply set the widget to override __init__and specify the form in your admin class. The specified form will be used only for 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 this helps you.

+1
source

All Articles