Login

Dynamically change admin widgets at runtime

Author:
davisd
Posted:
April 16, 2010
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

django-adminwidgetswap

adminwidgetswap is used for dynamically swapping out widgets from django's generated admin.

This allows applications to be packaged generically without the need for WYSIWYG dependencies editors- giving the application consumer the freedom to chose admin widgets without modifying original app source.

Author

David Davis (http://www.davisd.com) dynamically change django admin widets at runtime (django-adminwidgetswap) blog post

Usage

To change a widget in django's admin, just put adminwidgetswap.py on the python path, import adminwidgetswap.py and use:

adminwidgetswap.swap_model_field(model, field, widget)

...to change a widget for a direct model admin's field


adminwidgetswap.swap_model_inline_field(model, field, widget)

...to change widgets for inlines of a specific model and field


adminwidgetswap.swap_model_and_inline_fields(model, field, widget)

...to change both the widget for the direct model admin's field as well as all inline usages for the model and field


I usually have a project-level application called website, and I put this initialization code inside the website app's init.py

Usage - parameters

model is the Model class (eg. models.GalleryImage)

field is the field name you're looking to swap (eg. 'image')

widget is the widget you're going to swap for (eg. widgetlibrary.ThumbnailWidget())

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.