2

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 
Kashyap
  • 157
  • 3
  • 15

2 Answers2

4

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')

              ] 
JPG
  • 82,442
  • 19
  • 127
  • 206
  • 1
    Glad to know that :) Anyway, accept both answers if so, that's the StackOverflow strategy ;) – JPG Aug 31 '18 at 13:21
  • 1
    @Kashyap [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – JPG Aug 31 '18 at 13:22
  • Oops. Sorry I forgot – Kashyap Aug 31 '18 at 13:47
  • @JPG I got similar problem and your solution did worked and I was able to update the response. But in my case, I want to show a custom response message when the authentication fails, like: response={'result':0, 'message':'Bad Request'}, and when it passes its shoud give result 1 with a key value and user primary key. How could I achieve this. You could check my link of question asked- https://stackoverflow.com/questions/53319843/modify-response-from-django-rest-api-using-customusermodel-extending-default-use – Reema Parakh Nov 16 '18 at 05:34
  • What you mean by `authentication fails` ?? – JPG Nov 16 '18 at 05:46
  • Just like, if email is already registered, the registration fails. – Reema Parakh Nov 16 '18 at 05:51
  • So, If anything went wrong while the registration process, the response should include the info of the `USER`? Is that your requirement? – JPG Nov 16 '18 at 05:55
  • No, what I want is: In failure: response={'result':0, 'message':'Bad Request'}. and in case off success, I want: response={'result':1, 'key':'trtb468hsse4f3235235'} – Reema Parakh Nov 16 '18 at 05:59
0

The response is hardcoded and not configurable as you can see in function get_response_data in code:

https://github.com/Tivix/django-rest-auth/blob/master/rest_auth/registration/views.py

Your best bet is to create a custom class which extends the ExtendedRegisterView class and overrides get_response_data and/or create andmethods.

e.g.

class ExtendedRegisterView(MyRegisterSerializer):
    def get_response_data(self, user):
        if allauth_settings.EMAIL_VERIFICATION == \
                allauth_settings.EmailVerificationMethod.MANDATORY:
            # return custom answer
        else:
            return super(ExtendedRegisterView, self).get_response_data(user)

Then you also have to point the url to your new class (before the inclusion of other register api endpoints).

e.g.

url(r'^rest-auth/registration/$', ExtendedRegisterView.as_view(), name='rest_register'),)
url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))

Disclaimer: I didn't intend to write functional code, just some guidance how to solve your problem, there

miloslu
  • 434
  • 7
  • 12