0

I'm writing a simple django app using django 1.6 and python 2.7.2 and I wanted to create a Post object quickly so I could test something else I was writing. Used the built in django Admin page to add the object, and it threw an error saying 'int' object is not subscriptable. I'm sure I could create the object in another way if necessary, but I am wondering if this is something I am doing incorrectly that I should fix or if it is a bug I shouldn't worry about.

Models.py:

from django.db import models
from django.contrib.auth.models import User

class Forum(models.Model):
    name = models.CharField(max_length = 100)
    club = models.ForeignKey(Club, blank = True)
    public = models.BooleanField()

    def __unicode__(self):
        return self.name

class Thread(models.Model):
    name = models.CharField(max_length = 100)
    forum = models.ForeignKey(Forum)
    can_post = models.BooleanField()

    def __unicode__(self):
        return self.name

class Like(models.Model):
    user = models.ForeignKey(User)

    def __unicode__(self):
         return self.id

class Post(models.Model):
    body = models.TextField()
    poster = models.ForeignKey(User)
    thread = models.ForeignKey(Thread)
    forum = models.ForeignKey(Forum)
    likes = models.ManyToManyField(Like, blank = True)
    date_posted = models.DateTimeField(auto_now_add=True) 


    def __unicode__(self):
        return self.id

Traceback:

File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/contrib/admin/options.py" in wrapper
  430.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/contrib/admin/sites.py" in inner
  198.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/utils/decorators.py" in bound_func
  25.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/db/transaction.py" in inner
  339.                 return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/contrib/admin/options.py" in add_view
  1131.                 self.log_addition(request, new_object)
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/contrib/admin/options.py" in log_addition
  598.             action_flag=ADDITION
File "/usr/local/lib/python2.7/dist-packages/Django-1.6-py2.7.egg/django/contrib/admin/models.py" in log_action
  19.         e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)

Exception Type: TypeError at /admin/forums/post/add/
Exception Value: 'int' object is not subscriptable
1
  • Have you tried to change the unicode method of the Post class to return unicode(self.id)? Commented Jan 25, 2014 at 4:44

1 Answer 1

3

Try to return:

def __unicode__(self):
        return u"%s" % self.id

instead of

def __unicode__(self):
        return self.id

in the Post class

Python expects you to return a unicode string, but you are returning an int here.

You can also just return unicode(self.id)

Sign up to request clarification or add additional context in comments.

2 Comments

That worked perfectly. Thanks a lot. I figured i was doing something stupid.
Glad it works. Yes, especially cause we think that in Python, return an int as a string works, as it is not statically typed, but unicode is different ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.