twittergithub
rss

Third-party Widget choices (WYSIWYG Editors, etc.) should be left to an application consumer (the project), and not hard-coded in a reusable application. I need to be able to choose the editing mechanism I prefer for a particular client (Markdown, TinyMCE, CKEditor, etc.), and I shouldn't have to edit the original (reusable) django application to make it happen.

I would prefer that reusable applications ship with the most generic editing capability for rich text (A forms.Textarea) widget... But there should be a simple way to swap these default widgets for richer widgets at runtime.

There's no easy way that I could find in the current django framework to dynamically swap out a widget... So I wrote django-adminwidgetswap!

Here's a short snippet (adminwidgetswap.py) to dynamically change-out widgets at runtime within the django admin. Just put it on your python path and find a convenient place to:

import adminwidgetswap

adminwidgetswap.swap_model_field(models.YourModel, 'content', ContentEditingWidget())
# or 
adminwidgetswap.swap_model_inline_field(models.YourModel, 'content', ContentEditingWidget())
# or
adminwidgetswap.swap_model_and_inline_fields(models.YourModel, 'content', ContentEditingWidget())

I always have a project-specific application called website, and I put the above initialization code inside the website app's __init__.py

Enjoy!

adminwidgetswap.py
# django-adminwidgetswap by DavisD (David Davis) www.davisd.com
# v2.0, now with functionality!

from django.contrib import admin

# autodiscover the admin, very important
admin.autodiscover()

def __get_model_formfield_for_dbfield(model, field, widget):
    """
    Gets the new formfield_for_dbfield_function for a model
    """
    old_formfield_for_dbfield = admin.site._registry[model].formfield_for_dbfield
    def formfield_for_dbfield(db_field, **kwargs):
        if db_field.name == field:
            kwargs['widget'] = widget
        return old_formfield_for_dbfield(db_field, **kwargs)
    return formfield_for_dbfield

def __get_inline_formfield_for_dbfield(inline, field, widget):
    """
    Gets the new formfield_for_dbfield function for an inline
    """
    old_formfield_for_dbfield = inline.formfield_for_dbfield
    def formfield_for_dbfield(db_field, **kwargs):
        if db_field.name == field:
            kwargs['widget'] = widget
        return old_formfield_for_dbfield(db_field, **kwargs)
    return formfield_for_dbfield

def swap_model_field(model, field, widget):
    """
    Swaps an admin model field widget (not the inlines where the model is used)
    """
    if admin.site._registry.has_key(model):
        admin.site._registry[model].formfield_for_dbfield = __get_model_formfield_for_dbfield(model, field, widget)
    
def swap_model_inline_field(model, field, widget):
    """
    Swaps admin model inline field widget (not the direct model admin)
    """
    for registered_model in admin.site._registry:
        if admin.site._registry.has_key(registered_model):
            for inline in admin.site._registry[registered_model].inline_instances:
                if inline.model == model:
                    inline.formfield_for_dbfield = __get_inline_formfield_for_dbfield(inline, field, widget)

def swap_model_and_inline_fields(model, field, widget):
    """
    Swaps an admin model field widget as well as all inlines
    """
    swap_model_field(model, field, widget)
    swap_model_inline_field(model, field, widget)

Leave a comment!

Comments are moderated. If your comment does not appear immediately, give it a few, it may require authorization. If you're a spam-bot, you may not leave a comment, it is strictly forbidden in the Terms of Service.

You may comment using markdown syntax (link opens in new window).

David Davis