ModelSerializers do not behave in exactly the same way as ModelForm. However, there are intentional design decisions behind these differences. commit is not (and won't ever be) a keyword argument to save().
I believe that the usage of .save() is pretty much adequately documented here.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/38571245-f03d-454b-8d8b-5258af7ee8b3%40googlegroups.com.
serializers.py:
class ProfileSerializer(serializers.ModelSerializer):
user = UserCreateSerializer() # create the serializer for user
# mention all the other fields in profile model
class Meta:
model = Profile
fields = ('user',) # change this as per your requirement
def validate(self, attrs):
# validate the data if you have any conditions.
return attrs
def create(self, validated_data):
# pop out the username from dict to create profile data and then save the user.
user = validated_data.pop('username')
profile_obj = Profile.objects.create(**validated_data)
profile_obj.user = user
profile_obj.save()
return {"success": True} # You can return anything that you want so that you can send this in reponse from api.py.
api.py
def post(self, request):
request_data = request.data
request_data.update({user: request.user.username})
serializer = ProfileSerializer(data=request_data)
if serializer.is_valid():
return Response(serializer.validated_data)
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2699fc81-c792-4156-920f-70ead8edeff7%40googlegroups.com.
In that you can always call this method:
def get_serializer_class(self):
if self.request.user and self.request.user.is_authenticated:
return ProfileSerializer
else:
return UserSerializer