Django unique_together 不工作:"refers to the non-existent field"
Django unique_together does not work: "refers to the non-existent field"
在 Django 中创建模型,我需要使两个整数字段的组合唯一:
class example(models.Model):
lenght = models.PositiveSmallIntegerField
position = models.PositiveSmallIntegerField
otherfield = models.ForeignKey('onetable')
otherfield2 = models.ForeignKey('anothertable')
class Meta:
unique_together = (("lenght", "position"),)
所以当我同步数据库时,我收到以下错误消息:
正在执行 manage.py syncdb
SystemCheckError:系统检查发现了一些问题:
ERRORS:
prj.CodeBlock: (models.E012) 'unique_together' refers to the non-existent field 'lenght'.
prj.CodeBlock: (models.E012) 'unique_together' refers to the non-existent field 'position'.
The Python REPL process has exited
>>>
我发现如果我将字段类型更改为 "charfield" 我没有收到任何错误消息:
class example(models.Model):
lenght = models.CharField(max_length=8)
position = models.CharField(max_length=8)
otherfield = models.ForeignKey('onetable')
otherfield2 = models.ForeignKey('anothertable')
class Meta:
unique_together = (("lenght", "position"),)
为什么我不能使整数字段的组合唯一?
因为您没有声明(实例化)整数字段(您只是引用了它们的 classes):
class example(models.Model):
lenght = models.PositiveSmallIntegerField
position = models.PositiveSmallIntegerField
length
和 position
不是字段实例,而是字段 classes。尝试将它们实例化为 table:
中实际存在的字段
class example(models.Model):
lenght = models.PositiveSmallIntegerField()
position = models.PositiveSmallIntegerField()
在其 metaclass 中,Django 检测并枚举字段实例(即通过 运行 isinstance(v, Field)
)并创建它们的列。您可以在 class 中声明任何值(方法是属性;也许您的 class 具有自定义异常或 choices=
参数的常量值,...),但只有 Field 实例会被列举。这适用于字段 classes:Django 不会特殊对待它们:也许您在模型中将自定义 Field
class 声明为内部 class(仅用于在你的模型中),你不会期望它变成一个字段......所以这就是 Django 不将对字段的引用 classes 转换为对字段实例的引用的原因。
你必须明确。也许你忘记了括号。
在 Django 中创建模型,我需要使两个整数字段的组合唯一:
class example(models.Model):
lenght = models.PositiveSmallIntegerField
position = models.PositiveSmallIntegerField
otherfield = models.ForeignKey('onetable')
otherfield2 = models.ForeignKey('anothertable')
class Meta:
unique_together = (("lenght", "position"),)
所以当我同步数据库时,我收到以下错误消息:
正在执行 manage.py syncdb SystemCheckError:系统检查发现了一些问题:
ERRORS:
prj.CodeBlock: (models.E012) 'unique_together' refers to the non-existent field 'lenght'.
prj.CodeBlock: (models.E012) 'unique_together' refers to the non-existent field 'position'.
The Python REPL process has exited
>>>
我发现如果我将字段类型更改为 "charfield" 我没有收到任何错误消息:
class example(models.Model):
lenght = models.CharField(max_length=8)
position = models.CharField(max_length=8)
otherfield = models.ForeignKey('onetable')
otherfield2 = models.ForeignKey('anothertable')
class Meta:
unique_together = (("lenght", "position"),)
为什么我不能使整数字段的组合唯一?
因为您没有声明(实例化)整数字段(您只是引用了它们的 classes):
class example(models.Model):
lenght = models.PositiveSmallIntegerField
position = models.PositiveSmallIntegerField
length
和 position
不是字段实例,而是字段 classes。尝试将它们实例化为 table:
class example(models.Model):
lenght = models.PositiveSmallIntegerField()
position = models.PositiveSmallIntegerField()
在其 metaclass 中,Django 检测并枚举字段实例(即通过 运行 isinstance(v, Field)
)并创建它们的列。您可以在 class 中声明任何值(方法是属性;也许您的 class 具有自定义异常或 choices=
参数的常量值,...),但只有 Field 实例会被列举。这适用于字段 classes:Django 不会特殊对待它们:也许您在模型中将自定义 Field
class 声明为内部 class(仅用于在你的模型中),你不会期望它变成一个字段......所以这就是 Django 不将对字段的引用 classes 转换为对字段实例的引用的原因。
你必须明确。也许你忘记了括号。