Django Admin - Overriding the widget of a custom form field

前端 未结 4 1151
旧巷少年郎
旧巷少年郎 2020-12-02 14:54

I have a custom TagField form field.

class TagField(forms.CharField):
    def __init__(self, *args, **kwargs):
        super(TagField, self).__init__(*args,          


        
4条回答
  •  被撕碎了的回忆
    2020-12-02 15:19

    Try to change your field like this:

    class TagField(forms.CharField):
        def __init__(self, *args, **kwargs):
            self.widget = forms.TextInput(attrs={'class':'tag_field'})
            super(TagField, self).__init__(*args, **kwargs)
    

    This would allow to use the widget which comes from **kwargs. Otherwise your field will always use form.TextInput widget.

提交回复
热议问题