Reputation: 23
I am using django-stdimage2 to rename and resize images in my project. It comes with the ability to delete images through the Django Admin interface: simply tick a check box and click Save.
I can successfully add an image using the Django Admin but when I try to delete an image this error is returned:
Error Message
TypeError at /admin/app/gear_images/1/
coercing to Unicode: need string or buffer, NoneType found
Model
class gear_images(models.Model):
def __unicode__(self):
return self.image.name
gear_id = models.ForeignKey(gear)
image = StdImageField(upload_to='images/gear', blank=True, size=(640, 480, True), thumbnail_size=(100, 100, True))
description = models.CharField(max_length=100)
Possible Cause
I assume an error is returned because the image has already been deleted from the database, so there is nothing to return.
Fix?
What is the correct way to code for this situation?
Edit: Solution Found. Thank you, Vanessa!
def __unicode__(self):
if self.image is None:
return "None"
elif self.image.name is None:
return "None"
else:
return self.image.name
Thank you :-)
Upvotes: 2
Views: 3490
Reputation: 992
Since the image is not there, it is trying to convert "None" (a NULL item) to unicode, which isn't going to work. You can test it with:
def __unicode(self)__:
if self.image is None:
return "None"
else:
return self.image.name
Upvotes: 4