gpt4 book ai didi

javascript - DRF 在嵌套序列化器中创建用户

转载 作者:行者123 更新时间:2023-12-03 08:07:32 26 4
gpt4 key购买 nike

大家好,我正在尝试通过 API 在 django Rest 框架中使用嵌套序列化器创建用户,但我遇到了一些问题:

这是我的代码:

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',)

和我的第二个序列化器:

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

这是我的观点:

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?

我正在尝试通过 AngularJs POST 嵌套对象,但它说:

affiliate: ["This field is required."]

如果我通过 url:/api/affilaite 直接从后端进入,它会很好地注册用户,但我唯一遇到的问题是,

How to send the POST request with a nested object in javascript.

这是我的 JavaScript 代码:

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']
}
}

请帮助我:D 我正在挣扎:P

最佳答案

您有affiliate在序列化器中只读 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

关于javascript - DRF 在嵌套序列化器中创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com