DJANGO REST + DJOSER /api/auth/users/activation get invalid user or token wrong 错误

DJANGO REST + DJOSER /api/auth/users/activation get invalid user or token wrong error

我应该创建一个 auth get 方法,但是 /auth/users/activation 出现错误

{
   "uid": [
       "Invalid user id or user doesn't exist."
    ]
}

还有这个错误

{
  "detail": "Stale token for given user."
}

在电子邮件用户 ID 字段中是字符串,如“MTE”、“Mw”... 用户激活信号无效

Djoser 参数

DJOSER = {
  "LOGIN_FIELD": "email",
  "PASSWORD_CHANGED_EMAIL_CONFIRMATION": True,
  'PASSWORD_RESET_CONFIRM_URL': 'api/users/users/password/reset/confirm/{uid}/{token}',
  'USERNAME_RESET_CONFIRM_URL': 'api/users/users/username/reset/confirm/{uid}/{token}',
  'ACTIVATION_URL': 'api/users/users/activate/{uid}/{token}',
  'SEND_ACTIVATION_EMAIL': True,
  'SERIALIZERS': {
    "user_create": "restapi.app.serializers.RegistrationSerializer",
    "user": "restapi.app.serializers.UserSerializer",
    "current_user": "restapi.app.serializers.UserSerializer"
  },
  'PERMISSIONS': {
    'user_list': ['rest_framework.permissions.AllowAny'],
  }
}

restapi/app/serializers.py

from rest_framework import serializers
from restapi.app.models import *
from .docs import *
from django.contrib.auth.models import User

class PioneerProfileSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = PioneerProfile
        fields = ["elo", "max_elo", "min_elo"]

class UserSerializer(serializers.HyperlinkedModelSerializer):
    description = serializers.CharField(source="profile.description")
    pioneer = PioneerProfileSerializer()
    class Meta:
        model = User
        fields = ["id",
              "username",
              "first_name",
              "last_name",
              "description",
              "email",
              "date_joined",
              "pioneer"]

class RegistrationSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ["email", "username", "first_name", "last_name", "email", "password"]

    def validate_username(self, value):
        check_query = Genre.objects.filter(name=value)
        if self.instance:
            check_query = check_query.exclude(pk=self.instance.pk)

        if self.parent is not None and self.parent.instance is not None:
            genre = getattr(self.parent.instance, self.field_name)
            check_query = check_query.exclude(pk=genre.pk)

        if check_query.exists():
            raise serializers.ValidationError('A Genre with this name already exists.')

        return value

restapi/app/signal.py

from django.dispatch import receiver
from djoser.signals import user_activated
from .models import Profile
from .models import PioneerProfile

@receiver(user_activated)
def activate_profile(user, request):
    Profile.objects.create(user=user)
    PioneerProfile.objects.create(user=user)

restapi/app.py

from django.apps import AppConfig


class AccountsConfig(AppConfig):
    name = 'accounts'

    def ready(self):
        import restapi.app.signal

Created user permission

PioneerProfile 和 Profile 为空

问题已解决。使用 is_active=True 创建的标准 Django 用户,pre_save 信号解决了问题