gpt4 book ai didi

python - 期望一个字符缓冲区对象

转载 作者:太空狗 更新时间:2023-10-30 01:55:07 25 4
gpt4 key购买 nike

post 中的模型.在 admin.py 中:

class GroupsAdmin(admin.ModelAdmin):

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

]

所以,在这个管理部分,我可以将 first_year 添加到特殊系列的汽车中。我按“+”,然后打开一个新窗口 - 我可以在其中添加年份。我添加“2011”,按“保存”并看到错误:

TypeError: expected a character buffer object.

这很奇怪,因为它在上周工作。怎么修?如果我转到 first_yearAdmin,并添加年份,它可以正常工作。

谢谢。

编辑。回溯:

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

模型

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

最佳答案

实际上,如果您希望将year 字段转换为Integer,则没有必要将其转换为Charfield,因为在很多情况下您确实需要一个数字,这是不可能的。这个 1.5 错误的解决方法是让 __unicode__ 函数返回 unicode 字符串:

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

def __unicode__(self):
return unicode(self.year)

关于python - 期望一个字符缓冲区对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15162673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com