Last active
December 26, 2017 13:29
-
-
Save DimasInchidi/fd575553e8e36d24d0c1e16daa8ced55 to your computer and use it in GitHub Desktop.
kulgram gist 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#views.py | |
from rest_framework import generics, permissions | |
from portofool_io.serializers import UserProfileSerializer | |
class Registration(generics.CreateAPIView): | |
serializer_class = UserProfileSerializer | |
permission_classes = (permissions.AllowAny,) | |
# serializers.py | |
from django.contrib.auth.models import User | |
from rest_framework import serializers | |
from portofool_io.models import UserProfile | |
class UserSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = User | |
fields = ('username', 'first_name', 'last_name', 'email', 'password',) | |
extra_kwargs = { | |
'password': {'write_only': True} | |
} | |
class UserProfileSerializer(serializers.ModelSerializer): | |
user = UserSerializer() | |
class Meta: | |
model = UserProfile | |
fields = '__all__' | |
def create(self, validated_data): | |
user_data = validated_data.pop('user') | |
user = User.objects.create_user(is_active=False, **user_data) | |
customer = UserProfile.objects.create(user=user, **validated_data) | |
return customer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment