【问题标题】:Django rest framework authentication database errorDjango rest框架身份验证数据库错误
【发布时间】:2020-04-24 11:57:22
【问题描述】:

我正在尝试在我当前的项目中实现 django rest 框架。 api 引擎在没有安全性的情况下工作。我正在尝试按照 django rest 框架的身份验证页面中给出的步骤添加身份验证。

当我将以下部分添加到 settings.py 时,我没有收到任何错误:

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
    'rest_framework.authentication.TokenAuthentication',
]
}

但是当我将“rest_framework.authtoken”添加到 INSTALLED_APPS 并运行服务器时,

我收到以下消息:

您有 2 个未应用的迁移。在您为应用程序应用迁移之前,您的项目可能无法正常工作:authtoken。 运行“python manage.py migrate”以应用它们。

然后我尝试运行:

python manage.py migrate

我收到以下错误:

(raiotic-venv) 用户名​​@username-VirtualBox:~/Servers/Repositories/raiotic-venv/raiotic$ python manage.py migrate 要执行的操作: 应用所有迁移:MainApp、admin、auth、authtoken、contenttypes、sessions 运行迁移: 应用 authtoken.0001_initial...Traceback(最近一次调用最后): _execute 中的文件“/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py”,第 82 行 返回 self.cursor.execute(sql) 文件“/home/username/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py”,第 381 行,在执行 返回 Database.Cursor.execute(self, query) sqlite3.OperationalError:尝试写入只读数据库

上述异常是以下异常的直接原因:

Traceback(最近一次调用最后一次): 文件“manage.py”,第 21 行,在 主要的() 文件“manage.py”,第 17 行,在 main execute_from_command_line(sys.argv) 文件“/home/username/venv/lib/python3.6/site-packages/django/core/management/init.py”,第 381 行,在 execute_from_command_line 实用程序.execute() 文件“/home/username/venv/lib/python3.6/site-packages/django/core/management/init.py”,第 375 行,在执行 self.fetch_command(子命令).run_from_argv(self.argv) 文件“/home/username/venv/lib/python3.6/site-packages/django/core/management/base.py”,第 323 行,在 run_from_argv self.execute(*args, **cmd_options) 文件“/home/username/venv/lib/python3.6/site-packages/django/core/management/base.py”,第364行,在执行 输出 = self.handle(*args, **options) 文件“/home/username/venv/lib/python3.6/site-packages/django/core/management/base.py”,第 83 行,已包装 res = handle_func(*args, **kwargs) 文件“/home/username/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py”,第 234 行,在句柄中 fake_initial=fake_initial, 文件“/home/username/venv/lib/python3.6/site-packages/django/db/migrations/executor.py”,第 117 行,在 migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) _migrate_all_forwards 中的文件“/home/username/venv/lib/python3.6/site-packages/django/db/migrations/executor.py”,第 147 行 state = self.apply_migration(状态,迁移,fake=fake,fake_initial=fake_initial) 文件“/home/username/venv/lib/python3.6/site-packages/django/db/migrations/executor.py”,第 245 行,在 apply_migration state = migration.apply(state, schema_editor) 文件“/home/username/venv/lib/python3.6/site-packages/django/db/migrations/migration.py”,第 124 行,在应用中 operation.database_forwards(self.app_label,schema_editor,old_state,project_state) 文件“/home/username/venv/lib/python3.6/site-packages/django/db/migrations/operations/models.py”,第 92 行,在 database_forwards schema_editor.create_model(模型) 文件“/home/username/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py”,第 307 行,在 create_model self.execute(sql, params or None) 文件“/home/username/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py”,第 137 行,在执行 cursor.execute(sql,参数) 文件“/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py”,第 99 行,在执行 返回 super().execute(sql, 参数) 文件“/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py”,第 67 行,在执行 return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) _execute_with_wrappers 中的文件“/home/username/venv/lib/python3.6/site-packages/django/db/backends/utils.py”,第 76 行 返回执行程序(sql,参数,许多,上下文) _execute 中的文件“/home/username/venvlib/python3.6/site-packages/django/db/backends/utils.py”,第 84 行 返回 self.cursor.execute(sql, params) exit 中的文件“/home/username/venvlib/python3.6/site-packages/django/db/utils.py”,第 89 行 从 exc_value 提高 dj_exc_value.with_traceback(traceback) _execute 中的文件“/home/username/venvlib/python3.6/site-packages/django/db/backends/utils.py”,第 82 行 返回 self.cursor.execute(sql) 文件“/home/username/venvlib/python3.6/site-packages/django/db/backends/sqlite3/base.py”,第 381 行,在执行 返回 Database.Cursor.execute(self, query) django.db.utils.OperationalError: 尝试写入只读数据库

【问题讨论】:

    标签: django rest api django-rest-framework django-rest-auth


    【解决方案1】:

    尝试更改 db.sqlite3 文件的权限,这可能是您正在运行的用户的模式是只读的,因为没有写入文件的权限。

    ls -al db.sqlite3
    chmod a+w db.sqlite3
    python manage.py migrate
    

    【讨论】:

    • 感谢 TralahM。这是一个许可问题。但是,使用 chmod 更改权限并没有修复它。我可以看到权限已更改,但 django 没有响应。所以我删除了 sqlite 文件,然后将 django db 路径更改为不同的文件夹。执行了迁移。然后将其更改回原始位置并再次迁移。它现在似乎正在工作。
    猜你喜欢
    • 2015-08-16
    • 2013-06-29
    • 2017-09-26
    • 2017-11-02
    • 1970-01-01
    • 2019-03-01
    • 2021-12-21
    • 1970-01-01
    • 2022-06-25
    相关资源
    最近更新 更多