【发布时间】:2015-11-20 16:13:13
【问题描述】:
在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"),)
为什么我不能使整数字段的组合唯一?
【问题讨论】: