4

I am trying to implement authentication by combining Django Rest Framework and Angular, but I am suffering from user information update.
Angular sends it to Django with the PUT method, Django accepts the request with View "AuthInfoUpdateView".

class AuthInfoUpdateView(generics.GenericAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = AccountSerializer
    lookup_field = 'email'
    queryset = Account.objects.all()

    def put(self, request, *args, **kwargs):
        serializer = AccountSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

At this time, Django accepts the request as below.

request.data = {'email': '[email protected]', 'username': 'test3', 'profile': 'i am test3'}
request.user = [email protected]

And the serializer is implementing as below.

from django.contrib.auth import update_session_auth_hash
from rest_framework import serializers

from .models import Account, AccountManager

class AccountSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True, required=False)

    class Meta:
        model = Account
        fields = ('id', 'username', 'email', 'profile', 'password')

    def create(self, validated_data):
        return Account.objects.create_user(request_data=validated_data)

    def update(self, instance, validated_data):
        insntance.username = validated_data.get('username', instance.username)
        insntance.email = validated_data.get('email', instance.email)
        insntance.profile = validated_data.get('profile', instance.profile)
        instance = super().update(instance, validated_data)
        return instance

I tried to update the user from Angular in such an implementation, and the following response is returned.
"{"username":["account with this username already exists."],"email":["account with this email address already exists."]}"

It is thought that it is because you did not specify the record to update, but is there a way to solve it smartly without changing the current configuration so much?
I need your help.

3
  • Which version of django rest framework are you using? From 3.0, PUT is always considered as update django-rest-framework.org/api-guide/generic-views/… Commented Jun 21, 2017 at 4:08
  • @arun i'm using djangorestframework==3.6.3. Commented Jun 21, 2017 at 4:17
  • You wrote insntance instead of instance. If it is not a typo you're not modifying the object that you think. Commented Jun 21, 2017 at 5:18

1 Answer 1

2

use

class AuthInfoUpdateView(generics.UpdateAPIView):

use http method patch can partial_update your instance.

method PATCH -> partial update instance 
method PUT -> update instance
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.