Creating Model Form with python code in form

While I consider that sufficient reason itself to require the use of forms, especially in the case of data being submitted through an API, there are ancillary benefits as well.

  • Binds the submitted data to the underlying Python variables.
  • Provides for the transformation of data from Python variables to HTML
  • Provides the ability to track which fields were changed in a submit
  • In the case of model forms / formsets / inline formsets, provides an API for saving and retrieving data to the models, and managing foreign key relationships between related models.

A formset consists of a list of identical forms (and other stuff). A model formset contains a list of identical model forms.

So, a regular formset doesn’t contain models at all. It contains fields. Whether those fields represent fields in 0 models, 1 model, or n models is up to your code to manage.

A model formset contains fields from one model.

Beyond that, I suggest you read the entire page at Formsets | Django documentation | Django to see what all can be done to customize them.

(Now, while I could hypothesize the possibility of creating a formset consisting of different forms, perhaps using the get_form_kwargs parameter to create a formset in an ABABABAB pattern, I’ve never seen it done and I have no idea what problems would be encountered along the way.)

See Model formsets

Without seeing the error there’s no way to identify a cause.

However, you’re adding it to the context with two different keys. In your first assignment, the key is objectivetext_formset, and in the second, it’s formset. This would affect what your template needs to use.