我在 Django 中有以下模型:
class Priority(models.Model):
task = models.ForeignKey(Task, on_delete=models.CASCADE)
priority = models.PositiveSmallIntegerField
但是,当我运行 makemigrations 并迁移时,字段优先级 ( models.PositiveSmallIntegerField ) 不会出现在 mysql 表中,并且我无法创建该模型的对象。为什么?
最佳答案
因为您没有构造字段,您只传递了对该类的引用,因此您需要添加括号 (()
) 来进行调用:
class Priority(models.Model):
task = models.ForeignKey(Task, on_delete=models.CASCADE)
priority = models.PositiveSmallIntegerField<b>()</b>
# call the constructor ^^
如果您不调用构造函数,则已将 priority
设置为对 PositiveSmallIntegerField
class 的引用,而不是设置为 PositiveSmallIntegerField
对象。
关于python - Django 模型表不包含给定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56619668/