[Django]-Django Formset Missing Management Form Data

9👍

This error was caused by not using the proper prefix for the formset’s form data. Django’s inlineformset_factory automatically sets the formset prefix to the related_name defined in the ForeignKeyField on the child model.

models.py:

class BingoCardSquare(models.Model):

    ...

    card = models.ForeignKey(
        BingoCard,
        related_name='squares',
        on_delete=models.CASCADE,
    )
    ...

In this instance, the self.data attribute should have been set to the following:

self.data = {
    'squares-TOTAL_FORMS': '24',
    'squares-INITIAL_FORMS': '0',
    'squares-MAX_NUM_FORMS': '24',
    'squares-MIN_NUM_FORMS': '24'
    }

Each additional form would need to be prefixed the same way. Hopefully this answer will help a future djangonaut in distress.

👤Jay

Leave a comment