Django ModelForm fails validation with no errors

前端 未结 4 625
不知归路
不知归路 2021-02-14 02:37

Ok, I have been staring at this for hours trying to figure out what\'s going on, to no avail. I am trying to create a ModelForm using the \'instance\' keyword to pass it an exi

4条回答
  •  暖寄归人
    2021-02-14 03:10

    Note that your link doesn't call f.is_valid(), it just saves directly. This is potentially a bit misleading.

    The point is that instantiating a form with just an instance parameter but no data does not bind it to data, and the form is therefore not valid. You will see that f.is_bound is False.

    Behind the scenes, instance is really just the same as passing initial data, which as the docs note is only used to display the data initially and is not used for saving. You would probably benefit from reading the notes on bound and unbound forms.

提交回复
热议问题