Customizing a Django form widget? - Django
I have a little problem here!
I discovered the following as a globally accepted method for setting up a 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 be able to access another field of the model in order to decide how to display the field!
For instance:
If I'm tracking a value, let's call it " sales ".
If I want to customize the display of sales based on another field, let's call it " conversion rate ".
I don't have an obvious way to access the conversion field when overriding the sales widget!
Any ideas for working on this would be much appreciated! Thanks:)
a source to share
You are correct that the widgets themselves are independent. My first thought for doing something a little more complex is either to provide a custom admin template that does what you want, or to pass in some of the javascript code to handle related fields (similar to how pre-filled fields work).
a source to share