我无法弄清楚为什么我的makemigrations在django中不起作用,它显示跟随错误

时间:2022-12-05 23:09:15

here is my models.py file

这是我的models.py文件

from django.db import models



class Question(models.Model):
    question_text = models.CharField("question",max_length=200)
    pub_date = models.DateTimeField('date published')

    #def __str__(self):
        #return self.question_text


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

Here are the screenshots of errors

以下是错误的屏幕截图

Here is screenshot of my cmd

这是我的cmd的截图

Please help me to rectify this problem.

请帮我纠正这个问题。

2 个解决方案

#1


0  

Looking at your screenshot it seems, you did select 2nd option but never really added the default value here.

看看你的截图,你确实选择了第二个选项,但从未真正在这里添加默认值。

It seems you already had Choice model and then later you added choice_text field. The database need to add this column in the choice table so either you provide a default value or you choose to go with null=True

看来你已经有了Choice模型,后来又添加了choice_text字段。数据库需要在选择表中添加此列,以便您提供默认值,或者选择使用null = True

Add a default value to choice_field in Choice model:

在Choice模型中为choice_field添加默认值:

choice_text = models.CharField(max_length=200, default='your default value')

Or,

choice_text = models.CharField(max_length=200, null=True) 

#2


0  

我无法弄清楚为什么我的makemigrations在django中不起作用,它显示跟随错误

Your console clearly says,add some default value to choice_text field,this is happening because u may already have some data in Choice Model/Table and after migration when new non-nullable column is created ,it either wants to set default value from console or from python code :)

您的控制台清楚地说,向choice_text字段添加一些默认值,这是因为您可能已经在Choice Model / Table中有一些数据,并且在创建新的非可空列时迁移后,它要么想要从控制台设置默认值,要么来自python代码:)

#1


0  

Looking at your screenshot it seems, you did select 2nd option but never really added the default value here.

看看你的截图,你确实选择了第二个选项,但从未真正在这里添加默认值。

It seems you already had Choice model and then later you added choice_text field. The database need to add this column in the choice table so either you provide a default value or you choose to go with null=True

看来你已经有了Choice模型,后来又添加了choice_text字段。数据库需要在选择表中添加此列,以便您提供默认值,或者选择使用null = True

Add a default value to choice_field in Choice model:

在Choice模型中为choice_field添加默认值:

choice_text = models.CharField(max_length=200, default='your default value')

Or,

choice_text = models.CharField(max_length=200, null=True) 

#2


0  

我无法弄清楚为什么我的makemigrations在django中不起作用,它显示跟随错误

Your console clearly says,add some default value to choice_text field,this is happening because u may already have some data in Choice Model/Table and after migration when new non-nullable column is created ,it either wants to set default value from console or from python code :)

您的控制台清楚地说,向choice_text字段添加一些默认值,这是因为您可能已经在Choice Model / Table中有一些数据,并且在创建新的非可空列时迁移后,它要么想要从控制台设置默认值,要么来自python代码:)