How to pre-fill a ModelForm with existing data

What you have done here:

Is create a class-variable containing a blank instance of the form. This isn’t going to work the way you’re thinking it’s going to work.

A form is not a field.

:bulb: The light has gone on - now I think I understand what you’re trying to do.

Unfortunately, my response to this is “don’t do this”. Work with the two forms as two separate forms - do not try to integrate them like this. (This is an approach that I wouldn’t ever try to do.)

There’s a lot of “metaprogramming magic” that occurs within the Django Form class, and even more with ModelForms.

I have a philosophy that has served me well for a long time: “Don’t fight the framework.” Do things the way the framework expects you to do them, don’t try to twist it to make it fit your mental model - change your mental model instead.

See the post at How to handle forms that don't map easily to a single model for some ideas.

There are other threads in the forum itself addressing this type of situation.

1 Like