django sequence:item 找到 4 个预期的字符串或 unicode int

django sequence:item 4 expected string or unicode int found

我正在 运行 python manage.py 迁移我的模型,数据库已经制作好了,我现在只是为它制作模型, 但是当 运行 命令

时出现此错误
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 is not an explicit parameter of CharField, it thus performs a call to the __init__ of Field,其参数为:

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>)

This is more "self-explaining" as well: 一个没有太多Django经验的用户,通常可以猜到 max_length=3 意味着字符串可以最多包含三个字符,如果没有显式命名,he/she 必须查找此参数的确切含义。

我觉得你使用 DecimalFielddecimal_places=0 很奇怪,因为这基本上是一个 IntegerField,通常最好使用 IntegerField这种情况下,因为它更直接地映射到概念上的内容,数据库可以以更紧凑的方式存储它,并且算术运算通常在整数领域是正确的,而在浮点领域,可能会有舍入误差。