views:

308

answers:

1

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 RelatedFieldWidgetWrapper that is in django/contrib/admin/widgets.py.

So I wrote my version of this class and I changed its render function.

But now how can I use it? I mean... in the definition of my model do I have to use the formfield_overrides in this way?

formfield_overrides = {
        models.ForeignKey: {'widget': customRelatedFieldWidgetWrapper},
}

I think that this is not the right way, because that widget is not the one that manage the whole foreign key, but only the "+" icon. Am I wrong?

Thanks a lot.

+1  A: 

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
Dmitry Shevchenko
can you please write an example?
Giovanni Di Milia
edited my answer
Dmitry Shevchenko