美文网首页
makemigrations报错

makemigrations报错

作者: TTTRX | 来源:发表于2019-05-15 14:32 被阅读0次

    有关middleware的错误

    提出问题

    在运行python manage.py makemigrations出现了错误,错误信息如下:

    ERRORS:
    ?: (admin.E408) 'django.contrib.auth.middleware.AuthenticationMiddleware' must be in MIDDLEWARE in order to use the admin application.
    ?: (admin.E409) 'django.contrib.messages.middleware.MessageMiddleware' must be in MIDDLEWARE in order to use the admin application.
    ?: (admin.E410) 'django.contrib.sessions.middleware.SessionMiddleware' must be in MIDDLEWARE in order to use the admin application.

    解决方法

    之前我将MIDDLERWARE的内容全部注释掉了,然后加上了自己想要修改的内容,由于报错,因此我将上面三个取消注释,最后结果如下:

    MIDDLEWARE = [
    
        #'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        #'django.middleware.common.CommonMiddleware',
        #'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        #'django.middleware.clickjacking.XFrameOptionsMiddleware',
    
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'myblog', #新建立的app
    ]
    

    然后再尝试命令:
    python manage.py makemigrations
    成功运行。

    有关model默认值的问题

    提出问题

    我之前的Shoes类是这样的:

    class Shoes(models.Model):
        # 球鞋表的创建
    
        # 把id作为主键,考虑到后面网址的拼接id是str类型,
        # 所以这里主键设置为charfiled
        ShoesId=models.CharField(verbose_name='球鞋id',max_length=100,primary_key=True) #不要加default属性
        title=models.CharField(verbose_name='球鞋title',max_length=100)
        user=models.CharField(verbose_name='用户名称',max_length=100,default='root')
        class Meta:
            verbose_name = '球鞋id'
            verbose_name_plural = verbose_name
    
        def _str_(self):
            return self.title
    

    后来添加了一个属性叫number:
    number=models.CharField(verbose_name='球鞋货号',max_length=100)
    然后运行命令:python manage.py makemigrations
    这个报出错误:

    You are trying to add a non-nullable field 'number' to shoes without a default; we can't do that (the database needs something to populate existing rows).
    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
    Select an option:

    解决办法

    这种问题的解决办法一般是,将新增的属性设置一个默认值即可:
    number=models.CharField(verbose_name='球鞋货号',max_length=100,default=" ")

    支付宝红包码,你领红包我赚赏金;土豪请任意收钱码打赏

    相关文章

      网友评论

          本文标题:makemigrations报错

          本文链接:https://www.haomeiwen.com/subject/tcupaqtx.html