Django BigIntegerField无法通过def __unicode__(self):显示

0 投票
1 回答
936 浏览
提问于 2025-04-17 21:09

更新 #2: 我发现问题出在:

def __unicode__(self):
    return self.thread_id

我该如何返回线程ID,以便在管理面板中查看,使用 thread_id = models.IntegerField(unique=True) 呢?


在使用Django 1.6和Postgres时,当我保存一个条目时,我遇到了:

TypeError at /admin/bot_data/threadvault/add/
'int' object has no attribute '__getitem__'
Request Method: POST
Request URL:    http://10.14.213.69:8000/admin/bot_data/threadvault/add/
Django Version: 1.6.2
Exception Type: TypeError
Exception Value:    
'int' object has no attribute '__getitem__'
Exception Location: /home/one/.virtualenvs/bot/local/lib/python2.7/site-pac
kages/django/contrib/admin/models.py in log_action, line 19
    Python Executable:  /home/one/.virtualenvs/bot/bin/python
    Python Version: 2.7.3

这是我的models.py文件:

from django.db import models
from django.contrib.auth import get_user_model
from django.conf import settings
from django.forms import ModelForm

class UnassignedThread(models.Manager):
    def get_queryset(self):
        return super(UnassignedThread,
                self).get_queryset().filter(
                        user_irc_name__isnull=True)

def user_unicode_patch(self):
    return '%s %s' % (self.first_name, self.last_name)

get_user_model().__unicode__ = user_unicode_patch

class User(models.Model):
    user_ldap = models.ForeignKey(settings.AUTH_USER_MODEL,
            related_name='user_requester')
    user_irc_name = models.CharField(max_length="25")
    user_user_name = models.CharField(max_length="25")

class ThreadVault(models.Model):
    thread_id = models.BigIntegerField(unique=True)
    url = models.CharField(max_length="200")
    author_username = models.CharField(max_length="50")
    author_name = models.CharField(max_length="50")
    forum_id = models.CharField(max_length="50")
    subject = models.CharField(max_length="200")
    reply_count = models.CharField(max_length=("3"))
    latest_post_date = models.CharField(max_length=("50"))
    user_irc_name = models.ForeignKey(User, null=True, blank=True)

    objects = models.Manager()
    unassigned_threads = UnassignedThread()

    def __unicode__(self):
        return self.thread_id

不过,如果我把 thread_id = models.BigIntegerField(unique=True) 改成 thread_id = models.CharField(unique=True,max_length="50"),问题就解决了。为什么我不能使用BigIntegerField,怎样才能让它正常工作?我更希望使用整数类型,这样查找会更快。

更新:这是日志输出:

[06/Mar/2014 11:09:37] "GET /admin/jsi18n/ HTTP/1.1" 200 2372
Internal Server Error: /admin/bot_data/threadvault/add/
Traceback (most recent call last):
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 432, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 198, in inner
    return view(request, *args, **kwargs)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/utils/decorators.py", line 29, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in bound_func
    return func(self, *args2, **kwargs2)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/db/transaction.py", line 339, in inner
    return func(*args, **kwargs)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1133, in add_view
    self.log_addition(request, new_object)
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 600, in log_addition
    action_flag=ADDITION
  File "/home/one/.virtualenvs/bot/local/lib/python2.7/site-packages/django/contrib/admin/models.py", line 19, in log_action
    e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)
TypeError: 'int' object has no attribute '__getitem__'

1 个回答

2

我必须先把它转换成字符串:

def __unicode__(self):
    return str(self.thread_id)

撰写回答