The task is to create an API endpoint “/updateUser” for updating the user information. The API request should take the User object as input and update the user.
My model is this:
class UserModel(AbstractUser):
name = models.CharField(max_length=50)
email = models.EmailField(max_length=50, unique=True, primary_key=True)
phone = models.CharField(max_length=10, unique=True)
gender = models.CharField(max_length=10, null=True)
dob = models.DateField(null=True)
username = models.CharField(max_length=50, unique=True)
REQUIRED_FIELDS = []
Serializer is:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = UserModel
fields = ['id','name','username','email','phone','password']
extra_kwargs = {
'password': {'write_only': True}
}
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password !=None:
instance.set_password(password)
instance.save()
return instance
url is:
from django.urls import path
from . views import RegisterView, UpdateUserView
urlpatterns = [
path('register/', RegisterView.as_view()),
path('updateUser/', UpdateUserView.as_view())
]
Now, how to create view for this which takes object as input and update it?!