list_editable and widgets

后端 未结 2 1559
误落风尘
误落风尘 2021-02-03 10:28

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

2条回答
  •  难免孤独
    2021-02-03 11:00

    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.

提交回复
热议问题