Я пытаюсь настроить настраиваемый сериализатор входа в систему в Django и хочу настраиваемый ответ, но по умолчанию всегда отображается:
{
"username":[
"This field is required."
],
"password":[
"This field is required."
]
}
Я пытался настроить свой сериализатор следующим образом:
class MyLoginSerializer(serializers.Serializer):
username = serializers.CharField(required=True, allow_blank=True)
email = serializers.EmailField(required=False, allow_blank=True)
password = serializers.CharField(style={'input_type': 'password'})
def authenticate(self, **kwargs):
return authenticate(self.context['request'], **kwargs)
def _validate_email(self, email, password):
user = None
if email and password:
user = self.authenticate(email=email, password=password)
else:
msg = _('Must include "email" and "password".')
raise serializers.ValidationError(msg)
return user
def _validate_username(self, username, password):
print("in username")
user = None
if username and password:
print("in username 2")
try:
user = self.authenticate(username=username, password=password)
except Exception:
raise serializers.ValidationError("Wrong")
else:
print("in username 3")
msg = _('Must include "username" and "password".')
raise serializers.ValidationError(msg)
return user
def _validate_username_email(self, username, email, password):
user = None
if email and password:
user = self.authenticate(email=email, password=password)
elif username and password:
user = self.authenticate(username=username, password=password)
else:
msg = _(
'Must include either "username" or "email" and "password".'
)
raise serializers.ValidationError(msg)
return user
def validate(self, attrs):
username = attrs.get('username')
email = attrs.get('email')
password = attrs.get('password')
user = None
if 'allauth' in settings.INSTALLED_APPS:
from allauth.account import app_settings
# Authentication through email
if (app_settings.AUTHENTICATION_METHOD ==
app_settings.AuthenticationMethod.EMAIL):
user = self._validate_email(email, password)
# Authentication through username
elif (app_settings.AUTHENTICATION_METHOD ==
app_settings.AuthenticationMethod.USERNAME):
user = self._validate_username(username, password)
# Authentication through either username or email
else:
user = self._validate_username_email(username, email, password)
else:
# Authentication without using allauth
if email:
try:
username = GameUser.objects\
.get(email__iexact=email)\
.get_username()
except UserModel.DoesNotExist:
pass
if username:
user = self._validate_username_email(username, '', password)
# Did we get back an active user?
if user:
if not user.is_active:
msg = ('User account is disabled.')
raise exceptions.ValidationError(msg)
else:
msg = ('Wrong login information.')
raise exceptions.ValidationError(msg)
# If required, is the email verified?
if 'rest_auth.registration' in settings.INSTALLED_APPS:
from allauth.account import app_settings
if app_settings.EMAIL_VERIFICATION == app_settings\
.EmailVerificationMethod\
.MANDATORY:
email_address = user.emailaddress_set.get(email=user.email)
if not email_address.verified:
raise serializers.ValidationError((
'E-mail is not verified.'
))
attrs['user'] = user
return attrs
И у меня есть этот набор в качестве моего сериализатора входа в систему в моих settings.py:
REST_AUTH_SERIALIZERS ={
'LOGIN_SERIALIZER': 'api.serializer.MyLoginSerializer'
}
А вот мой пользовательский вид входа в систему:
class CustomLoginView(LoginView):
permission_classes = (AllowAny,)
serializer_class = MyLoginSerializer
def get_response(self):
original_response = super().get_response()
print("ORIGINAL REESPONSE:")
print(str(self.user))
mydata = {"username": str(self.user), "status": "success"}
original_response.data.update(mydata)
return original_response
Как бы Я получаю его, чтобы показать пользовательский 'Must include "email" and "password".'
вместо сообщения по умолчанию?
Спасибо!