Reputation:
Hello all i am trying to create an user in via API with nested serializers in django rest framework and i am having some issues:
Here is my code:
class AffiliateRegisterSerializer(serializers.ModelSerializer):
class Meta:
model = Affiliate
fields = ('phone','address','state','city','ZIP','country','company','web_name','web_url','web_desc','payee_name','monthly_visits',)
and my second serializer:
class UserSerializer(serializers.ModelSerializer):
'''
Registering a new user with Affiliate Profile
'''
affiliate = AffiliateRegisterSerializer(required=False)
class Meta:
model = User
fields = ('username','first_name','last_name','email','password','affiliate',)
write_only_fields = ('password',)
read_only_fields = ('id','affiliate',)
def create(self, validated_data):
affiliate_data = validated_data.pop('affiliate')
user = User.objects.create_user(**validated_data)
Affiliate.objects.create(user=user, **affiliate_data)
return user
and this is my view:
class AffiliateSignUp(generics.CreateAPIView):
'''
API endpoint for Affiliate Users registration
'''
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = [permissions.AllowAny]
@method_decorator(ensure_csrf_cookie)
@method_decorator(csrf_protect)
def create(self, request):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
How can i send an POST request via AngularJS to create the user and the profile instantly?
I am trying to POST a nested object via AngularJs but it says:
affiliate: ["This field is required."]
If i go directly from the backend via url: /api/affilaite it registers the user very good, but the only problem here which i am strugling is,
How to send the POST request with a nested object in javascript.
and here is my javascript code:
data:$httpParamSerializerJQLike({
'first_name':$scope.userData['first_name'],
'last_name':$scope.userData['last_name'],
'username':$scope.userData['username'],
'password':$scope.userData['password'],
'email':$scope.userData['email'],
affiliate:{
'phone':$scope.userData['phone'],
'address':$scope.userData['address'],
'state':$scope.userData['state'],
'city':$scope.userData['city'],
'ZIP':$scope.userData['ZIP'],
'country':$scope.userData['country'],
'company':$scope.userData['company'],
'web_url':$scope.userData['webUrl'],
'web_name':$scope.userData['webName'],
'web_desc':$scope.userData['webDesc'],
//'web_category':$scope.userData ['webCategory'],
'payee_name':$scope.userData['payeeName'],
'monthly_visits':$scope.userData['monthly_visits']
}
}
please help me guys :D i am struggling :P
Upvotes: 1
Views: 487
Reputation:
SOLVED
The problem was that i was sending an QueryDict application/x-www-form-urlencoded
so from the front-end i changed the
method:'POST',
url:'/api/URL/',
headers: {'Content-Type': 'application/json'},
dataType: 'json',
and i send a nested object.
So this does create a user with a user profile instantly.
thank you all for the support
Upvotes: 0
Reputation: 6255
You have affiliate
as read only in the serializer Meta
: read_only_fields = ('id','affiliate',)
.
read_only
Read-only fields are included in the API output, but should not be included in the input during create or update operations. Any 'read_only' fields that are incorrectly included in the serializer input will be ignored.
Set this to True to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization.
Defaults to False
Upvotes: 1