Issue
I am new to Django
My task is to do CRUD operations using serializers, but this error has come
here's my function:
def updateemp(request,id):
Updateemp = EmpModel.objects.get(id=id)
form = CRUDSerializer (request.POST,instance=Updateemp)
if form.is_valid():
form.save()
messages.success(request,'Record Updated Successfully...!:)')
return render(request,'Edit.html',{"EmpModel":Updateemp})
serializer:
class CRUDSerializer(serializers.ModelSerializer):
class Meta:
model = EmpModel
fields = "__all__"
below is the error:
BaseSerializer.__init__() got multiple values for argument 'instance'
can someone tell me where I am going wrong in the syntax?
Solution
If you decided to serializer an instance, you don't need to put keyword "instance" to the serializer.
Updateemp = EmpModel.objects.get(id=id)
form = CRUDSerializer(Updateemp)
Btw, EmpModel.objects.get(id=id)
might not return any object which could result in serializer error. Consider using get_object_or_404
or try catch
to prevent this error.
Answered By - Phan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.