gpt4 book ai didi

python - 由于 "coercing to Unicode"错误导致模型创建的所有实例

转载 作者:行者123 更新时间:2023-11-28 16:26:25 33 4
gpt4 key购买 nike

我有一个 Django 项目,其中包含两个功能性应用程序。今天,在添加两个新模型后,我意识到我的一个应用程序坏了。当我尝试向任何模型添加实例时,我收到与数据类型相关的错误。当我尝试在应用程序中创建任何模型的实例时,就会发生这种情况。当我尝试通过 Django 管理界面 创建实例时,甚至会发生这种情况!在管理界面中,我什至无法在不出现相同错误的情况下删除实例。

我已经复制了我的主要模型,以及创建实例和在管理界面中查看模型的回溯。

from django.db import models
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
import datetime

class DrukOrder(models.Model):
inkoop = models.CharField(max_length=16, default=0, unique=True)
order = models.IntegerField(default=0)
beschrijving = models.CharField(max_length=1024)
crediteur = models.IntegerField(default=0)
leverancier = models.CharField(max_length=1024, null=True)
medewerker = models.CharField(max_length=128, null=True)

besteldatum = models.DateField(null=True)
ontvangstdatum = models.DateField(null=True)
factuurdatum = models.DateField(null=True)
leverancierdatum = models.DateField(null=True)
oorspronkelijkedatum = models.DateField(null=True)

klant = models.CharField(max_length=1024, null=True)
kleuren = models.IntegerField(null=True)
rolbreedte = models.DecimalField(max_digits=5, decimal_places=2, default=0)
leveringweek = models.IntegerField(null=True)
materiaal = models.CharField(max_length=1024, null=True)
offerte = models.CharField(max_length=1024, null=True)
vrijetekst = models.CharField(max_length=2048, null=True)

datum = models.DateTimeField(auto_now=True, null=True)
user = models.ForeignKey(User, null=True)
aanmaakdatum = models.DateTimeField(null=True)
wijzigingdatum = models.DateTimeField(null=True)
status = models.CharField(max_length=128, null=True)
actief = models.BooleanField(default=True)
slug = models.SlugField(null=True)

class Meta:
unique_together = ('order', 'crediteur',)

def save(self, *args, **kwargs):
self.slug = slugify(self.inkoop)
super(DrukOrder, self).save(*args, **kwargs)

def __unicode__(self):
return self.id

在管理界面中加载模型的回溯:

Environment:


Request Method: GET
Request URL: http://pim.local:8000/admin/pam/drukorder/

Django Version: 1.8
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'registration',
'bootstrap3_datetime',
'pim',
'pam')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')


Template error:
In template /home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/contrib/admin/templates/admin/change_list.html, error at line 91
coercing to Unicode: need string or buffer, int found

81 : {% endif %}



82 : {% endblock %}



83 :



84 : <form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %} novalidate>{% csrf_token %}



85 : {% if cl.formset %}



86 : <div>{{ cl.formset.management_form }}</div>



87 : {% endif %}



88 :



89 : {% block result_list %}



90 : {% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}



91 : {% result_list cl %}



92 : {% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}



93 : {% endblock %}



94 : {% block pagination %}{% pagination cl %}{% endblock %}



95 : </form>



96 : </div>



97 : </div>



98 : {% endblock %}



99 :

Traceback:
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
164. response = response.render()
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/response.py" in render
158. self.content = self.rendered_content
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
135. content = template.render(context, self._request)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/backends/django.py" in render
74. return self.template.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in render
209. return self._render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
135. return compiled_parent._render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
135. return compiled_parent._render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
65. result = block.nodelist.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
65. result = block.nodelist.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/template/base.py" in render
1271. _dict = func(*resolved_args, **resolved_kwargs)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
320. 'results': list(results(cl))}
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
296. yield ResultList(None, items_for_result(cl, res, None))
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
287. super(ResultList, self).__init__(*items)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
199. f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/contrib/admin/utils.py" in lookup_field
282. value = attr()
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/db/models/base.py" in __str__
503. return force_text(self).encode('utf-8')
File "/home/administrator/.virtualenvs/pim/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
92. s = six.text_type(s)

Exception Type: TypeError at /admin/pam/drukorder/
Exception Value: coercing to Unicode: need string or buffer, int found

以及创建主模型实例时的错误:

TypeError: coercing to Unicode: need string or buffer, int found

我对这种突然的变化感到非常惊讶,它只影响了一个应用程序中的所有模型。我已经删除了新模型并删除了所有模型中的所有先前实例。创建新实例仍然失败,加载管理界面仍然失败。我应该从哪里开始解决这个问题?

最佳答案

__unicode__ 方法应该返回一个 Unicode 字符串,而不是一个整数。

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

关于python - 由于 "coercing to Unicode"错误导致模型创建的所有实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205459/

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