我正在使用django 1.6和python 2.7.2编写一个简单的django应用程序,我想快速创建一个Post
对象,这样我就可以测试我写的其他内容了。使用内置的django Admin页面添加对象,它引发了一个错误'int' object is not subscriptable
。我确信如果有必要,我可以用另一种方式创建对象,但我想知道这是否是我正在做的错误,我应该修复或如果它是一个我不应该担心的错误。
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
回溯:
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
答案 0 :(得分:2)
尝试返回:
def __unicode__(self):
return u"%s" % self.id
而不是
def __unicode__(self):
return self.id
Python希望你返回一个unicode字符串,但是你在这里返回一个int。
您也可以返回unicode(self.id)