Global formfield_overriding in django

I have my own text field widget and many forms in my project. To use this custom widget, I need to write:

formfield_overrides = { TextField: {'widget': CustomTextFieldWidget}, } 

in each form admin.ModelAdmin , and it is just ugly.

Is there a way to record it only once and use a custom widget for all forms in a project?

+7
source share
1 answer

No, there is no hook to redefine formfield widgets for the entire project.

You can make all classes of the model class inherited from the admin.ModelAdmin subclass, then you only need to set formfield_overrides once.

 class MyModelAdmin(admin.ModelAdmin): """ This is the parent class that all model admins in the project inherit from """ formfield_overrides = { TextField: {'widget': CustomTextFieldWidget}, } class AppleAdmin(MyModelAdmin): ... class BananaAdmin(MyModelAdmin): ... #etc 
+7
source

All Articles