Emile Emile - 5 months ago 33
Python Question

TypeError coercing to Unicode: need string or buffer

In a Django project, I am trying to pass the url to a want instance. Comments are applied to a Want. I have been trying to figure out this error but am stumped.

This function:

def comment_email(request, comment, **kwargs):
want = get_object_or_404(Want, id=comment.object_pk)
url = want.get_absolute_url
print url


Is throwing this error

Environment:

Request Method: POST
Request URL: http://localhost:8000/comments/post/
Django Version: 1.2.3
Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.comments',
'django.contrib.markup',
'src',
'lib.tagging',
'lib.markdown',
'lib.avatar',
'ajaxcomments',
'south']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ajaxcomments/utils.py" in wrapped
57. return func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/views/decorators/http.py" in inner
37. return func(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/comments/views/comments.py" in post_comment
127. request = request
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
162. response = receiver(signal=self, sender=sender, **named)
File "/Users/emilepetrone/code/apprentice2/src/utils.py" in comment_email
24. print url
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py" in __repr__
344. u = unicode(self)

Exception Type: TypeError at /comments/post/
Exception Value: coercing to Unicode: need string or buffer, Want found


Here is the Want model:

class Want(models.Model):

pub_date = models.DateTimeField(default=datetime.now,auto_now_add=True,db_index=True)
body = models.TextField(default='',max_length=1000)
body_html = models.TextField(editable=False, blank=True)

#Metadata
mentee = models.ForeignKey(User)
points = models.IntegerField(default=3)
enable_comments = models.BooleanField(default=True)
featured = models.BooleanField(default=False)

#Tags
tags = TagField(help_text="Autocomplete")

def get_tags(self):
return Tag.objects.get_for_object(self)

class Meta:
ordering = ['-pub_date']

def __unicode__(self):
return self

def save(self):
self.body_html = markdown(self.body)
super(Want, self).save()

def get_absolute_url(self):
return ( { 'object_id': self.id })
get_absolute_url = models.permalink(get_absolute_url)


Thank you for your help!

Answer

I think the problem is

def __unicode__(self):
    return self

which should return a unicode string rather than a "Want" instance. I'm not sure what you want there instead -- maybe "self.id"