TypeError:强制转换为Unicode:需要很长的字符串或缓冲区

时间:2016-12-08 08:32:41

标签: python unicode django-models typeerror coercion

class Movie(models.Model):
    movie_id = models.BigAutoField(primary_key=True)
    movie_name = models.CharField(max_length=128)
    movie_lang = models.CharField(max_length=36)
    director = models.CharField(max_length=72)
    producer = models.CharField(max_length=72)
    production_house = models.CharField(max_length=128)
    lead_actor = models.CharField(max_length=72)
    lead_actress = models.CharField(max_length=72)
    music_director = models.CharField(max_length=72)
    art_director = models.CharField(max_length=72)
    stunts = models.CharField(max_length=72)
    cinematography = models.CharField(max_length=72)
    costume_design = models.CharField(max_length=72)
    hair_stylist = models.CharField(max_length=72)  

    def __str__(self):
        return self.movie_name

class Cover(models.Model):
    cover_id = models.BigAutoField(primary_key=True)
    cover_path = models.CharField(max_length=512)
    movie = models.ForeignKey('Movie',on_delete=models.CASCADE) 

    def __str__(self):
        return self.cover_id

    def unicode(self):
        return unicode(self.cover_path)




#urls.py
from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^(?P<question_id>[0-9]+)/$',views.movie,name='movie'),
    url(r'^vedio/',views.vedio,name='vedio'),
    url(r'^image/',views.image,name='image'),
    url(r'^$',views.index,name = 'index'),
]

#views.py

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the movie station.")

def movie(request,question_id):
    return HttpResponse("Welcome to movies page")

def vedio(request):
    return HttpResponse("Welcome to vedios page")

def image(request):
    return HttpResponse("Welcome to images page")

环境:

请求方法:POST 请求网址:http://localhost:8000/admin/movieaware/cover/add/

Django版本:1.10.3 Python版本:2.7.12 已安装的应用程序:

['movieaware.apps.MovieawareConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']
  

回溯:

     

File&#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/core/handlers/exception.py"在内心     39. response = get_response(request)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/core/handlers/base.py"   在_get_response中     187. response = self.process_exception_by_middleware(e,request)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/core/handlers/base.py"   在_get_response中     185. response = wrapped_callback(request,* callback_args,** callback_kwargs)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py"   在包装中     544. return self.admin_site.admin_view(view)(* args,** kwargs)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py"   在_wrapped_view中     149. response = view_func(request,* args,** kwargs)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/views/decorators/cache.py"   在_wrapped_view_func中     57. response = view_func(request,* args,** kwargs)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/sites.py"   在内心     211.返回视图(请求,* args,** kwargs)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py"   在add_view中     1509. return self.changeform_view(request,None,form_url,extra_context)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py"   在_wrapper中     67. return bound_func(* args,** kwargs)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py"   在_wrapped_view中     149. response = view_func(request,* args,** kwargs)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py"   在bound_func中     63. return func。 get (self,type(self))(* args2,** kwargs2)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py"   在内心     185. return func(* args,** kwargs)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py"   在changeform_view中     1453. self.log_addition(request,new_object,change_message)

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py"   在log_addition中     719. object_repr = force_text(object),

     

文件   &#34; /home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/encoding.py"   在force_text中     78. s = six.text_type(s)

     

异常类型:/ admin / movieaware / cover / add / Exception中的TypeError   值:强制转换为Unicode:需要字符串或缓冲区,找到很久

我尝试了大多数解决方案,但没有任何效果。 我无法弄清楚问题

2 个答案:

答案 0 :(得分:0)

或许它可能是空的吗?

试试这个:

        return unicode(self.cover_path) or u''

如果您尝试通过&#34; unicode&#34;传递对象而不是字符串,也可能发生错误。

我们无法检查您的代码,但请测试类型(self.cover_path)并确保它是一个字符串,否则请手动转换以确保。

答案 1 :(得分:0)

def __str__(self):
    return self.cover_id

这里我为字符串函数返回了Long类型的id。它应该返回一个字符串,这就是它抛出错误的原因。