1

我正在使用 djoser 进行我的 rest api 身份验证

当我尝试向模型添加电话号码功能时,会发生以下错误:

    OperationalError at /api/users/
no such table: api_user

这是我的代码:

模型.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from phonenumber_field.modelfields import PhoneNumberField

class User(AbstractUser):
    email = models.EmailField(verbose_name='email',max_length=50,unique=True)
    phone = PhoneNumberField(unique=True,blank=False,null=False)

    REQUIRED_FIELDS = [
        'first_name'
        'last_name',
        'phone',
        'username',
    ]

    USERNAME_FIELD = 'email'

    def get_username(self):
        return self.email

序列化程序.py

from djoser.serializers import UserCreateSerializer,UserSerializer
from rest_framework import serializers
from .models import *

class UserCreateSerializer(UserCreateSerializer):
    class Meta(UserCreateSerializer.Meta):
        model = User
        fields = (
            'id',
            'email',
            'username',
            'password',
            'first_name',
            'last_name',
            'phone'
        )

设置.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES':(
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES':(
        'rest_framework.permissions.IsAuthenticated',
    ),
}

AUTH_USER_MODEL = 'api.User'

##  Djoser 
DJOSER = {
    'LOGIN_FIELD':'email',

    'SERIALIZERS': {
        'user_create':'api.serializers.UserCreateSerializer',
        'user': 'api.serializers.UserCreateSerializer',
    },
}

我将如何解决此错误。

另外,有没有办法从模型中删除用户名?我只想要电子邮件进行身份验证。

4

0 回答 0