Django Admin: Using a custom widget for only one model field

前端 未结 4 1959

I have a DateTimeField field in my model. I wanted to display it as a checkbox widget in the Django admin site. To do this, I created a custom form widget. However, I do not

4条回答
  •  隐瞒了意图╮
    2020-12-04 11:42

    Django's ModelAdmin.get_changelist_form(self, request, **kwargs) will do the trick for the case of list_editable

    class StopAdminForm(forms.ModelForm):
      class Meta:
        model = Stop
        widgets = {
          'approve_ts': ApproveStopWidget(),
        }
    
    class StopAdmin(admin.ModelAdmin):
      form = StopAdminForm
    
      #just return the ModelForm class StopAdminForm
      def get_changelist_form(self, request, **kwargs):
            return StopAdminForm
    

    Refer to Django Official documentation on this topic

    I hope this will help

提交回复
热议问题