由于“强制转换为Unicode”错误而导致的所有模型创建实例

时间:2016-03-24 16:39:47

标签: python django unicode

我有一个包含两个功能应用程序的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

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

1 个答案:

答案 0 :(得分:3)

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

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