开发者

list_editable and widgets

When using list_editable in ModelAdmin, is there any way to change the widget used for the editable fields? I can't find anything in the documentation. Seems like this would be something you'd be able to customiz开发者_开发百科e.


Override get_changelist_form method of your ModelAdmin class, like so:

def get_changelist_form(self, request, **kwargs):
  kwargs.setdefault('form', MyAdminForm)
  return super(MyModelAdmin, self).get_changelist_form(request, **kwargs)

And separately define the modified MyAdminForm:

class MyAdminForm(forms.ModelForm):
  class Meta:
     model = MyModel
  my_field = forms.DateField(widget=widgets.AdminDateWidget())

This is just an example which will make my_field represented by a widget for only the date (without time). That looks much better in the list view.


If you want to change widget only (tested on django 1.5) you won't need to set labels ect. just use this syntax:

from django import forms

class OfferForm(forms.ModelForm):
    class Meta:
        model = Offer
        widgets = {
            'description' : forms.Textarea(
                attrs={
                    'cols': '20',
                }
            )
        }

Where widgets dict is dict whose keys are names of fields and values represents widgets you want to override.

As documentation states for django 1.5+ its also possible to just:

class OfferAdmin(admin.ModelAdmin):
    def get_changelist_form(self, request, **kwargs):
        return OfferForm

Without any super() and kwargs juggling.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜