Search code examples
pythondjangoapidjango-rest-frameworkdjango-rest-auth

Is there any way to change view of Django-rest-auth of register?


I've created rest APIs using Django-rest-auth, in registration, it's returning

{
"detail": "Verification e-mail sent."
}

, But I need to add some status like success and message like email sent etc. Is there any way to override view of django-rest-auth for registration?

class MyRegisterSerializer(RegisterSerializer):
  first_name = serializers.CharField()
  last_name = serializers.CharField()

   def get_cleaned_data(self):
    super(MyRegisterSerializer, self).get_cleaned_data()
    return {
        'username': self.validated_data.get('username', ''),
        'password1': self.validated_data.get('password1', ''),
        'email': self.validated_data.get('email', ''),
        'first_name': self.validated_data.get('first_name', ''),
        'last_name': self.validated_data.get('last_name', '')
    }
def save(self, request):
    adapter = get_adapter()
    user = adapter.new_user(request)
    self.cleaned_data = self.get_cleaned_data() 
    adapter.save_user(request, user, self)
    setup_user_email(request, user, [])

    user.address = self.cleaned_data.get('address')
    user.user_type = self.cleaned_data.get('user_type')

    user.save()
    return user 

Solution

  • We could do it by overriding the corresponding view as we did here, Is there any way to change view of Django-rest-auth of login? .
    The difference is, the overriding function. Here we need to override the create() method as,

    from rest_auth.registration.views import RegisterView
    
    
    class CustomRegisterView(RegisterView):
        def create(self, request, *args, **kwargs):
            response = super().create(request, *args, **kwargs)
            custom_data = {"message": "some message", "status": "ok"}
            response.data.update(custom_data)
            return response

    and in urls.py

    urlpatterns = [
                      url(r'custom/registration/', CustomRegisterView.as_view(), name='my_custom_registration')
    
                  ]