I have custom textfield widget and many form in my project. To use this custom widget i need to to write:
formfield_overrides = {
TextField: {'widget': CustomTextFieldWidget},
}
in every admin.ModelAdmin
form, and that's just ugly.
Is there a way to write it just once and use custom widget across all forms in project?
No, there is no hook to override formfield widgets across an entire project.
You could make all of your model admin classes inherit from a subclass of admin.ModelAdmin
, then you only have 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With