3

I am trying to create a api for user Registration using the django rest framework.
I have the following models.py file

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE , primary_key = True)
    mobileNumber = models.IntegerField(default=0)
    avatar= models.ImageField(upload_to = 'User/' , default = '/static/User/defaultProfileImage.png')


def create_user_profile(sender, **kwargs):
    if kwargs['created']:
        profile = UserProfile.objects.create(user=kwargs['instance'])


post_save.connect(create_user_profile, sender=User)

This is my Serializers.py file

from rest_framework import serializers
from User.models import UserProfile
from django.contrib.auth.models import User





class UserSerializer(serializers.HyperlinkedModelSerializer):
    username = serializers.CharField()
    password1 = serializers.CharField(
        style={'input_type': 'password'},
        write_only=True)
    password2 = serializers.CharField(
        style={'input_type': 'password'},
        write_only=True)
    email = serializers.EmailField()
    class Meta:
        model = User
        fields = (
            'id',
            'username',
            'password1',
            'password2',
            'email',
            'first_name',
            'last_name',
            )

class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer()
    class Meta:
        model = UserProfile
        fields = (
            'user',
            'mobileNumber',
            'avatar')

And following is my views.py file

from User.models import UserProfile
from .serializers import UserProfileSerializer
from rest_framework.viewsets import ModelViewSet


class UserProfileViewSet(ModelViewSet):
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer

What is the best way to create a User Registeration using the api view that i have created. I tried many alternatives like overriding the create method in the UserProfile Serializer class and also the drf-writable-nested but got errors.
Please suggest me a way out. Also i want that the api is able to register users when called on by an android app.

1

1 Answer 1

1

You can do this in your Serializers.py file, this should work.

class UserSerializer(serializers.HyperlinkedModelSerializer):
    mobileNumber = serializers.IntegerField()
    avatar= serializers.ImageField()

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password', 'mobileNumber', 'avatar')

    def create(self, validated_data):
        mobile_number = validated_data.pop('mobileNumber', None)
        user = super(UserSerializer, self).create(validated_data)
        user.set_password(raw_password=validated_data['password'])
        user.save()
        userprofile = user.userprofile
        userprofile.mobileNumber = mobile_number
        userprofile.save()
        return user

    def update(self, instance, validated_data):
        mobile_number = validated_data.pop('mobileNumber', None)
        userprofile = instance.userprofile
        userprofile.mobileNumber = mobile_number
        userprofile.save()
        return super(UserSerializer, self).update(instance, validated_data)

Chuck the UserProfileSerializer for this use case, i feel here its not really needed. Your views.py and models.py look cool to me. Hope this helps you :-)

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.