ValueError on deleting a model object with an ImageField through Admin Interface

79 views
Skip to first unread message

ryan west

unread,
May 13, 2010, 10:45:14 AM5/13/10
to Django users
I'm having a silly issue with the admin interface and an ImageField in
one of my models. A simple model I have contains an ImageField, and
many objects of this model are related to some other model (Project,
not shown). I can add/view images just fine, but when I try to delete
an image, I get a value error saying:

"ValueError at /admin/projects/project/1/
The 'image' attribute has no file associated with it."

The entry correctly deletes from my DB (on successive loads of the
admin page, I no longer see the entry), the file does not delete from
my directory however, and this error pops up. Any thoughts on why?

I have tried with & without my custom delete function, with the same
results. Help would be appreciated, thanks.

Ryan

# ProjectImage Model
def get_project_image_path(instance, filename):
return os.path.join('projects', instance.project.name_slug,
filename)

class ProjectImage(models.Model):
project = models.ForeignKey(Project, verbose_name='project',
help_text='The project that this image is related to.')
image = models.ImageField(upload_to=get_project_image_path)
description = models.CharField(max_length=128, help_text='The image
description is a small text blurb used to help readers identify what
they\'re looking at when viewing an image.', blank=True)

class Meta:
verbose_name='project image'
verbose_name_plural='project images'

def __unicode__(self):
return self.image.path

def delete(self):
default_storage.delete(self.image.path)
super(ProjectImage, self).delete()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

ryan west

unread,
May 13, 2010, 12:33:26 PM5/13/10
to Django users
I found the solution to my own problem. When the page was reloading,
I am guessing that the actual entity had not been deleted yet, and the
method __unicode__(self) was being called, requesting access of
self.image (which did not exist, because that HAD been deleted).

A simple modification to the method seems to have fixed the problem:

def __unicode__(self):
if (self.image):
return self.image.path
else:
return 'No image exists... I should be deleted any moment!'
Reply all
Reply to author
Forward
0 new messages