Show a ManyToManyField as Checkboxes in Django Admin

后端 未结 4 1209
滥情空心
滥情空心 2021-02-02 09:32

Is there a simple way to show a ManyToManyField as Checkboxes in Django Admin? Thanks in advance!

4条回答
  •  深忆病人
    2021-02-02 09:56

    This is most certainly possible. Here is the code which you can place in the ModelAdmin subclass:

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        if db_field.name == 'your field name':
            kwargs['widget'] = form_widgets.CheckboxSelectMultiple()
            kwargs['help_text'] = ''
    
        return db_field.formfield(**kwargs)
    

    This was derived from looking into the admin code.

提交回复
热议问题