Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global formfield_overriding in django

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?

like image 594
nukl Avatar asked Dec 19 '11 14:12

nukl


1 Answers

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
like image 136
Alasdair Avatar answered Oct 22 '22 19:10

Alasdair