我正在运行 python manage.py migrate 和我的模型,数据库已经创建好了,我现在只是为它制作模型, 但是在运行命令时出现这个错误
TypeError: Error when calling the metaclass bases
sequence item 4: expected string or Unicode, int found
我的模型
class Publisher(models.Model):
publisherCode = models.CharField(3,primary_key=True)
publisherName = models.CharField(25)
city = models.CharField(20)
class Book(models.Model):
bookCode = models.CharField(4,primary_key=True)
title = models.CharField(40)
publisherCode = models.ForeignKey(Publisher)
type = models.CharField(3)
paperback = models.CharField(1)
class Branch(models.Model):
branchNum = models.DecimalField(2,0,primary_key=True)
branchName = models.CharField(50)
branchLocation = models.CharField(50)
class Copy(models.Model):
bookCode = models.ForeignKey(Book)
branchNum = models.ForeignKey(Branch)
copyNum = models.DecimalField(2,0,primary_key=True)
quality = models.CharField(20)
price = models.DecimalField(8,2)
最佳答案
简短回答:从 max_length
生成一个明确命名的参数。
基于source code , max_length
不是 CharField
的显式参数,因此它执行对 Field
的 __init__
的调用,这有作为参数:
def __init__(self, verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=NOT_PROVIDED, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text='', db_column=None, db_tablespace=None, auto_created=False, validators=(), error_messages=None):
如您所见,您的第一个未命名参数实际上将与 verbose_name
匹配,并且这应该是一个字符串。当然,将数字作为字符串提供将不会解决问题,因为那时您将 max_length
传递给 verbose_name
,而是 据我所知,>max_length
是 CharField
的必需参数。
解决方案是显式使用 max_length
,这样很明显您要将 4
分配给该参数。顺便说一句,DecimalField
也是如此:
class Publisher(models.Model):
publisherCode = models.CharField(<b>max_length=</b>3,primary_key=True)
publisherName = models.CharField(<b>max_length=</b>25)
city = models.CharField(<b>max_length=</b>20)
class Book(models.Model):
bookCode = models.CharField(<b>max_length=</b>4,primary_key=True)
title = models.CharField(<b>max_length=</b>40)
publisherCode = models.ForeignKey(Publisher)
type = models.CharField(<b>max_length=</b>3)
paperback = models.CharField(<b>max_length=</b>1)
class Branch(models.Model):
branchNum = models.DecimalField(<b>max_digits=2, decimal_places=0</b>, primary_key=True)
branchName = models.CharField(<b>max_length=</b>50)
branchLocation = models.CharField(<b>max_length=</b>50)
class Copy(models.Model):
bookCode = models.ForeignKey(Book)
branchNum = models.ForeignKey(Branch)
copyNum = models.DecimalField(<b>max_digits=2, decimal_places=0</b>, primary_key=True)
quality = models.CharField(<b>max_length=</b>20)
price = models.DecimalField(<b>max_digits=8, decimal_places=2</b>)
这也更“不言自明”:没有太多 Django 经验的用户通常可以猜到 max_length=3
意味着字符串可以包含在大多数三个字符,而没有显式命名,他/她应该必须查找此参数的确切含义。
我觉得你使用 DecimalField
和 decimal_places=0
很奇怪,因为这基本上是一个 IntegerField
,通常它更好在这种情况下使用 IntegerField
因为它更直接地映射到概念上的内容,数据库可以以更紧凑的方式存储它,并且算术运算通常在整数领域是正确的,而在浮点领域土地,可能存在舍入误差。
关于python - django 序列 :item 4 expected string or unicode int found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49694881/