Django 踩坑


在model.py中添加外码之后:

class Games(models.Model):
    name=models.CharField(max_length=50)
    version=models.IntegerField(default=0)
    company=models.CharField(max_length=50)
    def __str__(self):
        return self.name

class DLC(models.Model):
    game=models.ForeignKey(Games,on_delete=models.CASCADE)
    name=models.CharField(max_length=50)
    price=models.IntegerField(default=200)
    version=models.IntegerField(default=0)
    
class Test(models.Model):
    name=models.CharField(max_length=40)

执行:

 python manage.py makemigrations

出现:

Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)

 2) Quit, and let me add a default in models.py

可以在外码函数的参数中加入:

game=models.ForeignKey(Games,on_delete=models.CASCADE,null=True)

成功添加外码:





阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页