Django forms passed instance object gets overridden in is_valid

I was working on a Django view that updates the a certain model object x. In POST method, I initiate a ModelForm and pass an instance of the model object x. But when I try to use x object inside if form.is_valid(): block, some values in vars(x) go None. Here's the snippet to explain the scenario.

class MyUpdateView(generic.View):
    form_class = MyModelForm
    def post(self, request, *args, **kwargs):
        model_obj = MyModel.objects.get(id=kwargs.get("pk"))
        # Example: {"emp": 113, "project": 22, "salary": 28000}
        form = self.form_class(request.POST, instance=model_obj)
        print(vars(model_obj)) # works fine, shows all the data

        if form.is_valid():
            print(vars(model_obj)) # few values go None, not reusable
            # Example: {"emp": None, "project": None, "salary": 28000}

As we can see, when the vars checked inside the form.is_valid(), something happens to the passed instance object.

What could be possibly reason behind this?

Back to Top