Django 常见错误记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38276669/article/details/85273648

数据库问题

1. 配置问题

raise ImproperlyConfigured('mysqlclient 1.3.7 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.7 or newer is required; you have 0.7.10.None.

解决办法:

找到 /home/python/.virtualenvs/py3/lib/python3.5/site-packages/django/db/backends/mysql/base.pybase.py 文件

将文件中的如下代码注释即可:

if version < (1, 3, 7):
  raise ImproperlyConfigured('mysqlclient 1.3.7 or newer is required; you have %s.' % Database.__version__) 

2. 数据库迁移问题

错误

1. 外键设置默认值

You are trying to add a non-nullable field 'stage' to course without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
Provide a one-off default now (will be set on all existing rows with a null value for this column)
Quit, and let me add a default in models.py
Select an option: 

解决办法:

设置一个默认值,完成迁移后在模型中删除原来添加的默认值,并且在0001——initial.py中删除默认值

再执行python manage.py migrate 即可成功

2. 数据库迁移时报表已经存在

raise errorclass(errno, errval)
django.db.utils.InternalError: (1050, "Table 'user_operation_coursecomments' already exists")

解决办法:

python manage.py migrate users --fake

其中 users 是自己的子应用名称

或者: 将已经存在的这个表删除,再 migrate即可

3. 查询结果集排序造成的问题

UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'courses.models.Video'> QuerySet.
  paginator = self.django_paginator_class(queryset, page_size)

解决办法:

  1. queryset = Video.objects.all().order_by(‘id’) 将查询结果集排序
  2. class Meta:
    ​ ordering = [’-id’]
    ​ 加入ordering 排序字段

4. 错误提示如下:

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration user_operation.0001_initial is applied before its dependency sketch.0001_initial on database 'default'.

解决办法:

将之前迁移生成的表全部删掉(有auth_user表则保留),再重新开始数据库迁移操作

python manage.py makemigrations

python manage.py migrate

大功告成!!!

后续持续更新!!!

猜你喜欢

转载自blog.csdn.net/qq_38276669/article/details/85273648