Django: override RelatedFieldWidgetWrapper

后端 未结 2 822
不思量自难忘°
不思量自难忘° 2021-01-18 10:14

I want to change the way that the \"+\" icon for the foreign key in the admin site is shown.

I found that the widget that prints the code is RelatedFieldWidget

2条回答
  •  死守一世寂寞
    2021-01-18 11:00

    You would need to create custom ModelForm for ModelAdmin and override widget there.

    Example code:

    #forms.py
    class CustomForm(forms.ModelForm):
        user = forms.ModelChoiceField(queryset=User.objects.all(), widget=yourCustomWidget)
    
    class Meta:
        model = MyModel
    
    #admin.py
    class MyModelAdmin(admin.ModelAdmin):
         form = CustomForm
    

提交回复
热议问题