I’m trying to costomise the display in the admin interface. I need one of the fields to display the name and date submitted for photo sharing site I’m creating, but I keep getting the following error:
global name 'datetimeConvertToHumanReadable' is not defined
Here’s the full traceback: http://dpaste.com/822073/
My code
Model:
class Design(models.Model):
designer_id = models.ForeignKey(User)
date_submitted = models.DateTimeField()
title = models.CharField(max_length=70)
description = models.TextField()
photo = models.FileField(upload_to='design_photos')
def image_thumb(self):
return '<img src="/media/%s" width="100" height="100" />' % (self.photo)
image_thumb.allow_tags = True
def datetimeConvertToHumanReadable(dt):
# convert passed datetime to timestamp
dt_stamp = dt.strftime("%s")
# convert current datetime to timestamp
now = datetime.datetime.now()
now_stamp = now.strftime("%s")
# find difference between the two
delta = int(now_stamp) - int(dt_stamp)
# convert to (seconds ago, minutes ago, hours ago, etc...)
if (delta < 60):
return str(delta) + " seconds ago"
elif (delta < 3600):
return str(delta/60) + " minutes ago"
elif (delta < 86400):
return str(delta/3600) + " hours ago"
elif (delta < 31536000):
return str(delta/86400) + " days ago"
else:
return str(delta/31536000) + " years ago"
def name_and_submitted(self):
date_submitted = datetimeConvertToHumanReadable(date_submitted)
return '<div>%s<br />submitted %s ago</div>' % (self.designer_id, self.date_submitted)
def __unicode__(self):
return self.title
Admin.py:
class DesignAdmin(admin.ModelAdmin):
list_display = ('image_thumb', 'title', 'description', 'name_and_submitted')
The image_thumb method works fine to display a thumbnail in the field, but the name_and_submitted method throws up the error. Any ideas?
Just pass the
selfargument. That should solve the problemThe problem is that, when you are not passing the
selfargument, the function is not attached to the class and not callable through a model object. list_admin_display actually access the model instance and calls the function. In your case, it would be something likedesign_object.datetimeConvertToHumanReadablewhere design_object is each model instance displayed in the admin page. The argumentdtis not passed as an argument, so you need to extract it within your function.Hope this helps