mercredi 18 octobre 2017

Django Formset Missing Management Form Data

I'm attempting to write unit tests for a formset, but I'm getting the following error when trying to instantiate it.

error:

Traceback (most recent call last):
    File "/home/jwelborn/Documents/projects/Bingo/bingo/cards/tests/test_forms.py", line 218, in test_formset_accepts_valid_data
        self.assertTrue(formset.is_valid())
    File "/home/jwelborn/Documents/projects/Bingo/bingoenv/lib/python3.6/site-packages/django/forms/formsets.py", line 321, in is_valid
        self.errors
    File "/home/jwelborn/Documents/projects/Bingo/bingoenv/lib/python3.6/site-packages/django/forms/formsets.py", line 295, in errors
        self.full_clean()
    File "/home/jwelborn/Documents/projects/Bingo/bingoenv/lib/python3.6/site-packages/django/forms/formsets.py", line 343, in full_clean
        for i in range(0, self.total_form_count()):
    File "/home/jwelborn/Documents/projects/Bingo/bingoenv/lib/python3.6/site-packages/django/forms/formsets.py", line 116, in total_form_count
        return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max)
    File "/home/jwelborn/Documents/projects/Bingo/bingoenv/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
    File "/home/jwelborn/Documents/projects/Bingo/bingoenv/lib/python3.6/site-packages/django/forms/formsets.py", line 98, in management_form
        code='missing_management_form',
    django.core.exceptions.ValidationError: ['ManagementForm data is missing or has been tampered with']

This error says I'm missing ManagmentForm data, but I include it in the dictionary I'm passing to my form. I've checked different spellings and varying the min and max numbers, to no avail.

forms.py:

class BingoSquareForm(ModelForm):

    class Meta:
        model = BingoCardSquare
        exclude = ('created_date',)


BingoSquareFormset = inlineformset_factory(
    BingoCard,
    BingoCardSquare,
    form=BingoSquareForm,
    min_num=24,
    validate_min=True,
    max_num=24,
    validate_max=True
)

tests.py

class BingoSquareFormsetTests(TestCase):

    def setUp(self):

        self.user = User.objects.get_or_create(
            username='FormsetTestUser',
            email='something@yahoo.org'
        )[0]
        self.user.set_password('bingo')
        self.user.save()

        self.card = BingoCard.objects.get_or_create(
            title='FormsetTest',
            free_space='free_space',
            creator=self.user,
        )[0]

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

        for i in range(24):
            text_key = 'form-{}-text'.format(i)
            text_value = 'square {}'.format(i)
            self.data[text_key] = text_value

    def test_formset_accepts_valid_data(self):

        formset = BingoSquareFormset(self.data)
        self.assertTrue(formset.is_valid())

Can anyone see what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire