Customizing Django form widgets? - Django
Posted
by RadiantHex
on Stack Overflow
See other posts from Stack Overflow
or by RadiantHex
Published on 2010-05-04T15:52:33Z
Indexed on
2010/05/04
15:58 UTC
Read the original article
Hit count: 1028
Hi folks,
I'm having a little problem here!
I have discovered the following as being the globally accepted method for customizing Django admin field.
from django import forms
from django.utils.safestring import mark_safe
class AdminImageWidget(forms.FileInput):
"""
A ImageField Widget for admin that shows a thumbnail.
"""
def __init__(self, attrs={}):
super(AdminImageWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
output = []
if value and hasattr(value, "url"):
output.append(('<a target="_blank" href="%s">'
'<img src="%s" style="height: 28px;" /></a> '
% (value.url, value.url)))
output.append(super(AdminImageWidget, self).render(name, value, attrs))
return mark_safe(u''.join(output))
I need to have access to other field of the model in order to decide how to display the field!
For example:
If I am keeping track of a value, let us call it "sales".
If I wish to customize how sales is displayed depending on another field, let us call it "conversion rate".
I have no obvious way of accessing the conversion rate field when overriding the sales widget!
Any ideas to work around this would be highly appreciated! Thanks :)
© Stack Overflow or respective owner