__str__ returned non-string (type int)

简介: 打开微信扫一扫,关注微信公众号【数据与算法联盟】 转载请注明出处:http://blog.csdn.net/gamer_gyt 博主微博:http://weibo.com/234654758 Github:https://github.com/thinkgamer前言这个问题是我在做这个项目【点击查看】时遇到的,主要是因为以前在使用django的models时,在models的str(self) 函数时,默认返回的字段都是CharField类型的,而在这次返回了一个IntegerField类型导致出现了题目中的错误。


这里写图片描述
打开微信扫一扫,关注微信公众号【数据与算法联盟】

转载请注明出处: http://blog.csdn.net/gamer_gyt
博主微博: http://weibo.com/234654758
Github: https://github.com/thinkgamer


前言

这个问题是我在做这个项目【点击查看】时遇到的,主要是因为以前在使用django的models时,在models的str(self) 函数时,默认返回的字段都是CharField类型的,而在这次返回了一个IntegerField类型导致出现了题目中的错误。


python的Model._unicode_()和Model._str_()

__unicode__()

unicode() 方法在每当你对一个对象调用unicode() 时调用。Django 在许多地方都使用unicode(obj)(或者相关的函数 str(obj))。最明显的是在Django 的Admin 站点显示一个对象和在模板中插入对象的值的时候。所以,你应该始终让unicode() 方法返回模型的一个友好的、人类可读的形式。

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

如果你定义了模型的unicode() 方法且没有定义str() 方法,Django 将自动提供一个 str(),它调用unicode() 并转换结果为一个UTF-8 编码的字符串。下面是一个建议的开发实践:只定义unicode() 并让Django 在需要时负责字符串的转换。

__str__()

str() 方法在每当你对一个对象调用str() 时调用。在Python 3 中,Django 在许多地方使用str(obj)。 最明显的是在Django 的Admin 站点显示一个对象和在模板中插入对象的值的时候。 所以,你应该始终让str() 方法返回模型的一个友好的、人类可读的形式。

例如:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

    def __str__(self):
        return '%s %s' % (self.first_name, self.last_name)

在Python 2 中,Django 内部对str 的直接使用主要在随处可见的模型的repr() 输出中(例如,调试时的输出)。如果已经有合适的unicode() 方法就不需要str() 了。

前面unicode() 的示例可以使用str() 这样类似地编写:

from django.db import models
from django.utils.encoding import force_bytes

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

    def __str__(self):
        # Note use of django.utils.encoding.force_bytes() here because
        # first_name and last_name will be unicode strings.
        return force_bytes('%s %s' % (self.first_name, self.last_name))

发现问题和解决问题

遇到这个问题是因为在__str__() return 了 IntegerField 类型的数据

TypeError: __str__ returned non-string (type int)

解决办法:
return 的时候 加一个 str() 进行强制类型转换即可


Over !

相关文章
|
10月前
|
Go
go string to int 字符串与整数型的互换
go string to int 字符串与整数型的互换
50 0
|
4月前
|
前端开发 Java 数据库连接
Spring Boot 升级 3.2 报错 Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String
Spring Boot 升级 3.2 报错 Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String
|
1月前
|
Dart JavaScript 前端开发
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
52 4
|
1月前
|
SQL
Typecho——Argument 1 passed to Typecho\Router::get() must be of the type string
Typecho——Argument 1 passed to Typecho\Router::get() must be of the type string
16 0
|
2月前
|
存储 数据处理 索引
数据类型转换:int()、str()、float()
在Python中,数据类型转换是一项基础且重要的操作
遍历字符串,String line = xxx for(int i = 0;i<line.length();i++){system.out.println(line.chartAt(i)); 单个
遍历字符串,String line = xxx for(int i = 0;i<line.length();i++){system.out.println(line.chartAt(i)); 单个
|
2月前
|
存储 Python
语音输入,python数据类型,type()用来查看数据类型,数据类型转换,int(x)转整数,float(x)转换为浮点数,str(x),将对象转为字符串,标识符,标识符不允许使用关键字,关键字参考
语音输入,python数据类型,type()用来查看数据类型,数据类型转换,int(x)转整数,float(x)转换为浮点数,str(x),将对象转为字符串,标识符,标识符不允许使用关键字,关键字参考
|
3月前
|
Java
String转化为Int
String转化为Int
|
4月前
channelSftp.put(InputStream src, String dst, int mode);里的mode都是什么类型的
【5月更文挑战第15天】channelSftp.put(InputStream src, String dst, int mode);里的mode都是什么类型的
67 2
|
4月前
int 和 String 互相转换的多种方法
int 和 String 互相转换的多种方法
27 1