Message from discussion
Overriding an admin widget?
Received: by 10.52.33.4 with SMTP id n4mr6902588vdi.1.1335375496058;
Wed, 25 Apr 2012 10:38:16 -0700 (PDT)
X-BeenThere: django-users@googlegroups.com
Received: by 10.220.219.141 with SMTP id hu13ls119535vcb.8.gmail; Wed, 25 Apr
2012 10:37:54 -0700 (PDT)
Received: by 10.52.88.201 with SMTP id bi9mr6865553vdb.6.1335375473935;
Wed, 25 Apr 2012 10:37:53 -0700 (PDT)
Received: by 10.52.88.201 with SMTP id bi9mr6865549vdb.6.1335375473884;
Wed, 25 Apr 2012 10:37:53 -0700 (PDT)
Return-Path: <r...@panix.com>
Received: from mailbackend.panix.com (mailbackend.panix.com. [166.84.1.89])
by gmr-mx.google.com with ESMTP id cl2si190352vdc.3.2012.04.25.10.37.53;
Wed, 25 Apr 2012 10:37:53 -0700 (PDT)
Received-SPF: pass (google.com: domain of r...@panix.com designates 166.84.1.89 as permitted sender) client-ip=166.84.1.89;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of r...@panix.com designates 166.84.1.89 as permitted sender) smtp.mail=...@panix.com
Received: from sullivan.lic.songza.com (unknown [66.114.87.114])
by mailbackend.panix.com (Postfix) with ESMTP id 6C53634829;
Wed, 25 Apr 2012 13:37:53 -0400 (EDT)
Subject: Re: Overriding an admin widget?
Mime-Version: 1.0 (Apple Message framework v1257)
Content-Type: multipart/alternative; boundary="Apple-Mail=_01D9EBD2-31E7-42F7-9C1D-EA674AEFA53F"
From: Roy Smith <r...@panix.com>
In-Reply-To: <9E8A8AC9-B375-47EC-A601-1A191C90C7E7@panix.com>
Date: Wed, 25 Apr 2012 13:37:52 -0400
Message-Id: <FA6BB581-2EDC-4C22-A62B-AFE4C6763F85@panix.com>
References: <EC2A79A5-8CF1-49FC-A11C-8C9E31DF398F@panix.com> <9E8A8AC9-B375-47EC-A601-1A191C90C7E7@panix.com>
To: Django users <django-users@googlegroups.com>
X-Mailer: Apple Mail (2.1257)
--Apple-Mail=_01D9EBD2-31E7-42F7-9C1D-EA674AEFA53F
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=us-ascii
Oh, I see what's going on. I marked the field as readonly. Apparently =
the widget only gets created if the field is editable (I guess that =
makes sense).
On Apr 25, 2012, at 1:28 PM, Roy Smith wrote:
> Ooops, forgot to mention, I'm using django 1.4.
>=20
>=20
> On Apr 25, 2012, at 12:51 PM, Roy Smith wrote:
>=20
>> I'm trying to get the admin to give me a thumbnail for an ImageForm. =
Following the suggestion at http://djangosnippets.org/snippets/1580/, =
I've got the following code:
>>=20
>> from django import forms
>> from django.contrib import admin
>> from django.db import models
>> from django.utils.safestring import mark_safe
>>=20
>> class AdminImageWidget(forms.FileInput):
>> def __init__(self, attrs=3D{}):
>> assert 0
>> super(AdminImageWidget, self).__init__(attrs)
>>=20
>> def render(self, name, value, attrs=3DNone):
>> output =3D []
>> if value and hasattr(value, "url"):
>> output.append(('<a target=3D"_blank" href=3D"%s">'
>> '<img src=3D"%s" style=3D"height: 28px;" =
/></a> '
>> % (value.url, value.url)))
>> output.append(super(AdminImageWidget, self).render(name, =
value, attrs))
>> return mark_safe(u''.join(output))
>>=20
>> class ImageVersionAdmin(admin.ModelAdmin):
>> readonly_fields =3D ('image',
>> 'data')
>> formfield_overrides =3D {
>> models.ImageField: {'widget': AdminImageWidget,
>> 'label': 'My Label',},
>> }
>>=20
>> admin.site.register(ImageVersion, ImageVersionAdmin)
>>=20
>> The problem is, I'm still getting the standard admin display. I =
stuck the "assert 0" in AdminImageWidget.__init__() just to prove that =
it's never being called. What magic am I missing?
>>=20
>> My model looks like:
>>=20
>> class ImageVersion(Model):
>> image =3D ForeignKey(Image)
>> data =3D ImageField(upload_to=3D"images/foo")
>>=20
>> ---
>> Roy Smith
>> r...@panix.com
>>=20
>>=20
>>=20
>=20
>=20
> ---
> Roy Smith
> r...@panix.com
>=20
>=20
>=20
---
Roy Smith
r...@panix.com
--Apple-Mail=_01D9EBD2-31E7-42F7-9C1D-EA674AEFA53F
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
charset=us-ascii
<html><head></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Oh, I =
see what's going on. I marked the field as readonly. =
Apparently the widget only gets created if the field is editable =
(I guess that makes =
sense).<div><br></div><div><br></div><div><br><div><div>On Apr 25, 2012, =
at 1:28 PM, Roy Smith wrote:</div><br =
class=3D"Apple-interchange-newline"><blockquote type=3D"cite"><div =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; ">Ooops, forgot to mention, I'm =
using django 1.4.<div><br></div><div><br><div><div>On Apr 25, 2012, at =
12:51 PM, Roy Smith wrote:</div><br =
class=3D"Apple-interchange-newline"><blockquote type=3D"cite"><div =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; ">I'm trying to get the admin to =
give me a thumbnail for an ImageForm. Following the suggestion =
at <a =
href=3D"http://djangosnippets.org/snippets/1580/">http://djangosnippets.or=
g/snippets/1580/</a>, I've got the following =
code:<div><br></div><div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;"><div>from django import forms</div><div>from =
django.contrib import admin</div><div>from django.db import =
models</div><div>from django.utils.safestring import =
mark_safe</div></span></font></div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px; "><br></span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;">class =
AdminImageWidget(forms.FileInput):</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;"> def =
__init__(self, attrs=3D{}):</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;"> =
assert 0</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;"> =
super(AdminImageWidget, =
self).__init__(attrs)</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: =
11px;"><br></span></font></div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;"> def render(self, name, value, =
attrs=3DNone):</span></font></div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;"> output =3D =
[]</span></font></div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;"> if value and =
hasattr(value, "url"):</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;"> =
output.append(('<a target=3D"_blank" =
href=3D"%s">'</span></font></div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;"> =
'<img src=3D"%s"=
style=3D"height: 28px;" /></a> '</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;"> =
=
% (value.url, value.url)))</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;"> =
output.append(super(AdminImageWidget, self).render(name, =
value, attrs))</span></font></div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;"> return =
mark_safe(u''.join(output))</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: =
11px;"><br></span></font></div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;">class =
ImageVersionAdmin(admin.ModelAdmin):</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;"> =
readonly_fields =3D ('image',</span></font></div><div><span =
class=3D"Apple-style-span" style=3D"font-family: 'Courier New'; =
font-size: 11px; "> =
'data')</span></div><div><span =
class=3D"Apple-style-span" style=3D"font-family: 'Courier New'; =
font-size: 11px; "> formfield_overrides =3D =
{</span></div><div><font class=3D"Apple-style-span" face=3D"'Courier =
New'"><span class=3D"Apple-style-span" style=3D"font-size: 11px;"> =
models.ImageField: {'widget': =
AdminImageWidget,</span></font></div><div><font class=3D"Apple-style-span"=
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;"> =
'label': 'My =
Label',},</span></font></div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;"> =
}</span></font></div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;"><br></span></font></div><div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: =
11px;">admin.site.register(ImageVersion, =
ImageVersionAdmin)</span></font></div></div><div><br></div><div>The =
problem is, I'm still getting the standard admin display. I stuck =
the "assert 0" in AdminImageWidget.__init__() just to prove that it's =
never being called. What magic am I =
missing?</div><div><br></div><div>My model looks =
like:</div><div><br></div><div><div><font class=3D"Apple-style-span" =
face=3D"'Courier New'"><span class=3D"Apple-style-span" =
style=3D"font-size: 11px;">class =
ImageVersion(Model):</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;"> =
image =3D ForeignKey(Image)</span></font></div><div><font =
class=3D"Apple-style-span" face=3D"'Courier New'"><span =
class=3D"Apple-style-span" style=3D"font-size: 11px;"> data =
=3D =
ImageField(upload_to=3D"images/foo")</span></font></div><div><br></div></d=
iv><div apple-content-edited=3D"true"><div style=3D"word-wrap: =
break-word; -webkit-nbsp-mode: space; -webkit-line-break: =
after-white-space; "><div style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; "><div>---</div><div>Roy =
Smith</div><div><a =
href=3D"mailto:r...@panix.com">r...@panix.com</a></div><div><br></div></div>=
</div></div><br class=3D"Apple-interchange-newline">
</div>
<br></div></div></blockquote></div><br><div apple-content-edited=3D"true">=
<span class=3D"Apple-style-span" style=3D"border-collapse: separate; =
font-family: Helvetica; font-style: normal; font-variant: normal; =
font-weight: normal; letter-spacing: normal; line-height: normal; =
orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; =
widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span =
class=3D"Apple-style-span" style=3D"border-collapse: separate; =
font-family: Helvetica; font-style: normal; font-variant: normal; =
font-weight: normal; letter-spacing: normal; line-height: normal; =
orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; =
widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; "><span class=3D"Apple-style-span" =
style=3D"border-collapse: separate; font-family: Helvetica; font-style: =
normal; font-variant: normal; font-weight: normal; letter-spacing: =
normal; line-height: normal; orphans: 2; text-indent: 0px; =
text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; =
-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =
0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span =
class=3D"Apple-style-span" style=3D"border-collapse: separate; =
font-family: Helvetica; font-style: normal; font-variant: normal; =
font-weight: normal; letter-spacing: normal; line-height: normal; =
orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; =
widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span =
class=3D"Apple-style-span" style=3D"border-collapse: separate; =
font-family: Helvetica; font-style: normal; font-variant: normal; =
font-weight: normal; letter-spacing: normal; line-height: normal; =
orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; =
widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; "><span class=3D"Apple-style-span" =
style=3D"border-collapse: separate; font-family: Helvetica; font-style: =
normal; font-variant: normal; font-weight: normal; letter-spacing: =
normal; line-height: normal; orphans: 2; text-indent: 0px; =
text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; =
-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =
0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; "><div><br =
class=3D"Apple-interchange-newline">---</div><div>Roy Smith</div><div><a =
href=3D"mailto:r...@panix.com">r...@panix.com</a></div><div><br></div></div>=
</span></div></span></span></span></div></span></span><br =
class=3D"Apple-interchange-newline">
</div>
<br></div></div></blockquote></div><br><div apple-content-edited=3D"true">=
<span class=3D"Apple-style-span" style=3D"border-collapse: separate; =
color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; =
font-variant: normal; font-weight: normal; letter-spacing: normal; =
line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; =
text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; =
-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: =
0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span =
class=3D"Apple-style-span" style=3D"border-collapse: separate; color: =
rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: =
normal; font-weight: normal; letter-spacing: normal; line-height: =
normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: =
normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: =
0px; -webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; "><span class=3D"Apple-style-span" =
style=3D"border-collapse: separate; color: rgb(0, 0, 0); font-family: =
Helvetica; font-style: normal; font-variant: normal; font-weight: =
normal; letter-spacing: normal; line-height: normal; orphans: 2; =
text-indent: 0px; text-transform: none; white-space: normal; widows: 2; =
word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span =
class=3D"Apple-style-span" style=3D"border-collapse: separate; color: =
rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: =
normal; font-weight: normal; letter-spacing: normal; line-height: =
normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: =
normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: =
0px; -webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span =
class=3D"Apple-style-span" style=3D"border-collapse: separate; color: =
rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: =
normal; font-weight: normal; letter-spacing: normal; line-height: =
normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: =
normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: =
0px; -webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; "><span class=3D"Apple-style-span" =
style=3D"border-collapse: separate; color: rgb(0, 0, 0); font-family: =
Helvetica; font-style: normal; font-variant: normal; font-weight: =
normal; letter-spacing: normal; line-height: normal; orphans: 2; =
text-indent: 0px; text-transform: none; white-space: normal; widows: 2; =
word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div =
style=3D"word-wrap: break-word; -webkit-nbsp-mode: space; =
-webkit-line-break: after-white-space; "><div><br =
class=3D"Apple-interchange-newline">---</div><div>Roy Smith</div><div><a =
href=3D"mailto:r...@panix.com">r...@panix.com</a></div><div><br></div></div>=
</span></div></span></span></span></div></span></span><br =
class=3D"Apple-interchange-newline">
</div>
<br></div></body></html>=
--Apple-Mail=_01D9EBD2-31E7-42F7-9C1D-EA674AEFA53F--