【问题标题】:Django BigIntegerField Can Not Be Displayed With def __unicode__(self):Django BigIntegerField 不能用 def __unicode__(self) 显示:
【发布时间】:2014-03-06 17:17:44
【问题描述】:

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

def __unicode__(self):
    return self.thread_id

如何使用thread_id = models.IntegerField(unique=True) 返回thread_id 编号以供管理面板查看?


使用 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__'

【问题讨论】:

  • 您能否显示错误所提及的log_action?你是在覆盖任何保存还是只是打电话给foobar.save()
  • 这是我点击保存时的管理页面。对于“线程 ID”字段,我输入了 777。

标签: python django postgresql


【解决方案1】:

我必须先转换成字符串:

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

【讨论】:

    猜你喜欢
    • 2016-01-24
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 2021-09-05
    • 2015-11-08
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多