<commit_before>.. _formsets:

========
Formsets
========

Formsets are a great tool in Django to be able to quickly edit related objects,
or edit a set of objects on one page. They are frequently used to be able to
add an arbitry number of objects, or delete some objects.

Formsets operate with a prefix and an index to be able to distinguish the
different forms (each 'formset form' represents an object, or maybe just an
arbitrary form).

Formsets combined with Javascript to add/remove forms in the page without a full
page reload are powerfull, but crufty: you need to manually keep track of the
number of forms and make sure that the indices don't make any 'jumps', else
Django might complain.

The ``admin`` has a JS module to handle that fairly generic:
``django.contrib.admin.static.admin.js.inlines``, but it dictates a certain
markup.

The ``Formset`` class from ponyJS is a more generic implementation. It's an OOP
orientation to handle adding/removing forms painless.


Basic usage
===========

Usage is simple: import the class and instantiate an instance.

It does require you to specify the template for a new form, because no
assumptions are made whether you use HTML templates, React or any other tool.
See :ref:`formset-template` for more details.

The constructor takes two arguments, the first one is required: the ``prefix``
of the formset. The second one is an optional object with :ref:`options <formset-options>`.


.. code-block:: js

    import Formset from 'ponyjs/forms/formsets.js';

    let formset = Formset('form');


.. _formset-add-form:

Adding a form
-------------

Adding a form takes care of rendering the template to a new form and incrementing
the total form count in the formset management form. It's your responsibility<commit_msg>Fix docs<commit_after>.. _formsets:

========
Formsets
========

Formsets are a great tool in Django to be able to quickly edit related objects,
or edit a set of objects on one page. They are frequently used to be able to
add an arbitry number of objects, or delete some objects.

Formsets operate with a prefix and an index to be able to distinguish the
different forms (each 'formset form' represents an object, or maybe just an
arbitrary form).

Formsets combined with Javascript to add/remove forms in the page without a full
page reload are powerfull, but crufty: you need to manually keep track of the
number of forms and make sure that the indices don't make any 'jumps', else
Django might complain.

The ``admin`` has a JS module to handle that fairly generic:
``django.contrib.admin.static.admin.js.inlines``, but it dictates a certain
markup.

The ``Formset`` class from ponyJS is a more generic implementation. It's an OOP
orientation to handle adding/removing forms painless.


Basic usage
===========

Usage is simple: import the class and instantiate an instance.

It does require you to specify the template for a new form, because no
assumptions are made whether you use HTML templates, React or any other tool.
See :ref:`formset-template` for more details.

The constructor takes two arguments, the first one is required: the ``prefix``
of the formset. The second one is an optional object with :ref:`options <formset-options>`.


.. code-block:: js

    import Formset from 'ponyjs/forms/formsets.js';

    let formset = new Formset('form');


.. _formset-add-form:

Adding a form
-------------

Adding a form takes care of rendering the template to a new form and incrementing
the total form count in the formset management form. It's your responsibility
{'fraction_non_alphanumeric': 0.07837609247251198, 'fraction_numerical': 0.0, 'mean_word_length': 4.134587554269175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '608858', 'n_tokens_mistral': 956, 'n_tokens_neox': 910, 'n_words': 527}