Problem with ImageField and inline admin form

300 views
Skip to first unread message

Ramin Miraftabi

unread,
Aug 30, 2008, 1:44:59 AM8/30/08
to django...@googlegroups.com
Hi.

I'm stumped and can't figure out a solution for the following:

With the models:

class Entry(models.Model):
    pub_date = models.DateTimeField('date published')
    created = models.DateTimeField(auto_now_add=True, editable=False)
    edited = models.DateTimeField(auto_now=True, editable=False)
    title = models.CharField(max_length=200, blank=True)
    title_fi = models.CharField(max_length=200, blank=True)
    is_memorial = models.BooleanField('Use special layout?', default=False)

class Image(models.Model):
    entry = models.ForeignKey(Entry, related_name='images')
    file = models.ImageField(upload_to="%Y")
    caption = models.TextField()
    caption_fi = models.TextField()

And forms:

class ImageForm(forms.ModelForm):
    file = forms.ImageField()
    caption = forms.CharField(label='Default caption', widget=forms.Textarea)
    caption_fi = forms.CharField(label='Caption in Finnish', required=False, widget=forms.Textarea)

class ImageInline(admin.TabularInline):
    model = Image
    extra = 1
    form = ImageForm

class EntryForm(forms.ModelForm):
    pub_date = forms.DateField(label='Publication date', initial=datetime.date.today() + datetime.timedelta(1))
    title = forms.CharField(max_length=200, required=False)
    title_fi = forms.CharField(label='Finnish title', max_length=200, required=False)
    is_memorial = forms.BooleanField(label='Use special layout?', initial=False, required=False)
    tags = forms.CharField(max_length=200,required=False)

class EntryAdmin(admin.ModelAdmin):
    date_hierachy = 'pub_date'
    list_display = ('pub_date', 'title')
    list_filter = ('pub_date',)
    ordering = ('-pub_date',)
    search_fields = ('pub_date', 'title')
    inlines = [
        ImageInline,
    ]
    form = EntryForm

I can easily start creating a new Entry in admin, but trying to open a previously created entry results in the following error:

Template error

In template /usr/lib/python2.5/site-packages/django/contrib/admin/templates/admin/edit_inline/tabular.html, error at line 24

Caught an exception while rendering: coercing to Unicode: need string or buffer, ImageFieldFile found

14 {% endfor %}
15 {% if inline_admin_formset.formset.can_delete %}<th>{% trans "Delete?" %}</th>{% endif %}
16 </tr></thead>
17
18 {% for inline_admin_form in inline_admin_formset %}
19
20 <tr class="{% cycle row1,row2 %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}">
21
22 <td class="original">
23 {% if inline_admin_form.original or inline_admin_form.show_url %}<p>
24 {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}

In Django Alpha 1 this wasn't a problem (in fact I didn't even need to define a form for the Image model since in the model I could define old stuff).
With Django 8730 this error appears.

Any ideas?

ramin
--
Ramin Miraftabi http://fierymill.net/ramin/
blog @ http://randomfire.fierymill.net/

Kolme flättiä ja kultsu: http://fierymill.net/loj/
Three flatcoats and a golden: http://fierymill.net/loj/

Aaron

unread,
Sep 2, 2008, 5:32:11 PM9/2/08
to Django users
Hi,

I have similar models and wanted to do the same thing. Here's how I
got my inline to work (using your provided models as an example).

class Entry(models.Model):
pub_date = models.DateTimeField('date published')
created = models.DateTimeField(auto_now_add=True, editable=False)
edited = models.DateTimeField(auto_now=True, editable=False)
title = models.CharField(max_length=200, blank=True)
title_fi = models.CharField(max_length=200, blank=True)
is_memorial = models.BooleanField('Use special layout?',
default=False)

class Image(models.Model):
entry = models.ForeignKey(Entry, related_name='images')
file = models.ImageField(upload_to="%Y")
caption = models.TextField()
caption_fi = models.TextField()

class ImageInline(admin.TabularInline):
model = Image
extra = 1

class EntryAdmin(admin.ModelAdmin):
date_hierachy = 'pub_date'
list_display = ('pub_date', 'title')
list_filter = ('pub_date',)
ordering = ('-pub_date',)
search_fields = ('pub_date', 'title')
inlines = [
ImageInline,
]


============================

The point is that I didn't need to specify the forms explicitly for
the inline.

Hope this helps.

For reference, check out: http://www.djangoproject.com/documentation/admin/
> error at line *24*
> Caught an exception while rendering: coercing to Unicode: need string or
> buffer, ImageFieldFile found 14 {% endfor %} 15 {% if
> inline_admin_formset.formset.can_delete %}<th>{% trans "Delete?" %}</th>{%
> endif %} 16 </tr></thead> 17
> 18 {% for inline_admin_form in inline_admin_formset %} 19
> 20 <tr class="{% cycle row1,row2 %} {% if inline_admin_form.original or
> inline_admin_form.show_url %}has_original{% endif %}"> 21
> 22 <td class="original"> 23 {% if inline_admin_form.original or
> inline_admin_form.show_url %}<p> 24 {% if inline_admin_form.original %} {{
> inline_admin_form.original }}{% endif %}
> In Django Alpha 1 this wasn't a problem (in fact I didn't even need to
> define a form for the Image model since in the model I could define old
> stuff).
> With Django 8730 this error appears.
>
> Any ideas?
>
> ramin
> --
> Ramin Miraftabihttp://fierymill.net/ramin/
> blog @http://randomfire.fierymill.net/

Ramin Miraftabi

unread,
Sep 3, 2008, 2:57:17 AM9/3/08
to django...@googlegroups.com
The problem with this issue finally had nothing to do with the forms themselves, but the __unicode__ method of the Image-model. 

Older implementations of ImageField had a __unicode__ method of their own so 

def __unicode__(self):
    return self.file

worked fine. Now this needs to be

def __unicode__(self):
    return self.file.name

to achieve the same result. 

ramin
--
Ramin Miraftabi http://fierymill.net/ramin/
Reply all
Reply to author
Forward
0 new messages