开发者

Customizing Django form widgets? - Django

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 a开发者_如何转开发ccess 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 :)


You are correct that the widgets themselves are independent. My first thought for doing something more complex is to either provide a custom admin template that does what you want, or to pass in a piece of javascript code to handle the interrelated fields (much like how prepopulated fields work).


you probably want to use a custom form for the admin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜