Django Rest Framework没有创建表authtoken_token

时间:2016-08-16 18:43:48

标签: django django-rest-framework

我试图设置Django Rest Framework authtoken,根据我的理解,应在authtoken_tokenmakemigrations之后创建表migrate。我将rest_framework添加到settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Site',
    'rest_framework',
    'rest_framework.authtoken',
    'MySQLdb'
]

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAdminUser'
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
}

迁移命令输出:

'manage.py@MySite > makemigrations Site
"C:\Program Files (x86)\JetBrains\PyCharm 2016.2\bin\runnerw.exe" C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2016.2\helpers\pycharm\django_manage.py" makemigrations Site "C:/Users/Eric Franzen/PycharmProjects/MySite"
No changes detected in app 'Site'

Process finished with exit code 0
manage.py@MySite > migrate Site
"C:\Program Files (x86)\JetBrains\PyCharm 2016.2\bin\runnerw.exe" C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2016.2\helpers\pycharm\django_manage.py" migrate Site "C:/Users/Eric Franzen/PycharmProjects/MySite"
Operations to perform:
  Apply all migrations: Site
Running migrations:
  No migrations to apply.

流程已完成,退出代码为0' 如果有人知道为什么没有创建该表,那么信息将会受到赞赏!

3 个答案:

答案 0 :(得分:3)

正在运行migrate authtoken修复此

答案 1 :(得分:1)

由于未创建表的类似问题,我使用migrate authtoken

但是在运行测试时,迁移会失败,因为我的应用程序又进行了一次迁移,以将令牌存储在超级用户上; manage.py test没有按我需要的顺序运行迁移。

在我的情况下,解决方案是向需要Token的应用迁移添加依赖项,如下所示:

dependencies = [
        ('api', '0005_auto_20200221_2108'), # the prior migration
        ('authtoken', '0002_auto_20160226_1747'), # the last authtoken migration I saw in showmigrations
    ]

添加依赖性后,迁移成功运行,包括运行测试时,并且可以从工作流程中删除migrate authtoken

我不确定这是否解决了为什么在原始情况下根本不运行authtoken迁移的谜,但这也许有助于解决问题。

答案 2 :(得分:0)

您实际上可以直接直接运行sql查询。我正在使用pgadmin。

在必要时替换。

CREATE TABLE public.authtoken_token
(
    key character varying(40) COLLATE pg_catalog."default" NOT NULL,
    created timestamp with time zone NOT NULL,
    user_id integer NOT NULL,
    CONSTRAINT authtoken_token_pkey PRIMARY KEY (key),
    CONSTRAINT authtoken_token_user_id_key UNIQUE (user_id),
    CONSTRAINT authtoken_token_user_id_35299eff_fk_dashboard_dashboarduser_id FOREIGN KEY (user_id)
        REFERENCES public.dashboard_dashboarduser (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        DEFERRABLE INITIALLY DEFERRED
)

TABLESPACE pg_default;

ALTER TABLE public.authtoken_token
    OWNER to postgres;
-- Index: authtoken_token_key_10f0b77e_like

-- DROP INDEX public.authtoken_token_key_10f0b77e_like;

CREATE INDEX authtoken_token_key_10f0b77e_like
    ON public.authtoken_token USING btree
    (key COLLATE pg_catalog."default" varchar_pattern_ops ASC NULLS LAST)
    TABLESPACE pg_default;