简体   繁体   English

预期的字符缓冲区对象

[英]Expected a character buffer object

Models in this post . 这篇文章中的模型。 In admin.py: 在admin.py中:

class GroupsAdmin(admin.ModelAdmin):

    fieldsets = [
        (None,               {'fields': ['first_year', 'many other']}),

    ]

So, in this admin section I can add first_year to the special series of cars. 因此,在此管理部分中,我可以将first_year添加到特殊系列的汽车中。 I press "+", then opens a new window - where I can add the year. 我按“ +”,然后打开一个新窗口-我可以在其中添加年份。 I add '2011', press "Save" and see the error: 我添加“ 2011”,按“保存”,然后看到错误:

TypeError: expected a character buffer object.

Its very strange, because it works at last week. 这很奇怪,因为它在上周有效。 How it fix? 如何解决? If I go to the first_yearAdmin , and add the year, it works without errors. 如果我转到first_yearAdmin并添加年份,则可以正常运行。

Thanks. 谢谢。

Edit. 编辑。 Traceback: 追溯:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/cars/first_year/add/?_popup=1

Django Version: 1.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sitemaps',
 'cars',
 'django_evolution',
 'django.contrib.admin')
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 "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
  372.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
  202.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner
  223.                 return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in add_view
  1010.                 return self.response_add(request, new_object)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in response_add
  833.                 (escape(pk_value), escapejs(obj)))
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in wrapper
  194.             return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/html.py" in escapejs
  65.     return mark_safe(force_text(value).translate(_js_escapes))

Exception Type: TypeError at /admin/cars/first_year/add/
Exception Value: expected a character buffer object

Models 楷模

class Mark(models.Model):
    many_many_char_fields
    def __unicode__(self):
        return self.name

class First_Year(models.Model):
    year = models.IntegerField()
    def __unicode__(self):
        return str(self.year)

class Groups(models.Model):
        many_other_fields
    mark = models.ForeignKey(Mark, related_name='groups')
    last_update = models.DateTimeField()
    first_year = models.ForeignKey(First_Year, related_name='first_year')
    def __unicode__(self):
        return self.name
    def altered_date(self, year):
        altered_year = int(year)-int(self.first_year.year)
        return altered_year

Actually, it is not necessary to transform the year field into Charfield, if you want to keep it Integer, as it would be impossible in many cases where you really need a number. 实际上,如果要将year字段保留为整数,则不必将year字段转换为Charfield,因为在很多情况下,您确实需要数字时,这是不可能的。 The workaround for this 1.5 bug is to make __unicode__ function return unicode string: 这个1.5错误的解决方法是使__unicode__函数返回unicode字符串:

class First_Year(models.Model):
    year = models.IntegerField()

    def __unicode__(self):
        return unicode(self.year)
class First_Year(models.Model):
    year = models.CharField(max_length=4)
    def __unicode__(self):
        return self.year

you forget this 你忘了这个

class GroupsAdmin(admin.ModelAdmin):
    model = Groups

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM