Error : Serializer getting multiple values for argument 'instance'

Question:

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?

Asked By: Ameya Potdar

||

Answers:

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
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.