gpt4 book ai didi

python - Django Formset 缺少管理表单数据

转载 作者:行者123 更新时间:2023-11-28 21:39:26 25 4
gpt4 key购买 nike

我正在尝试为表单集编写单元测试,但在尝试实例化它时出现以下错误。

错误:

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']

此错误表明我缺少 ManagmentForm 数据,但我将其包含在传递给我的表单的字典中。我检查了不同的拼写并改变了最小和最大数字,但都无济于事。

表单.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
)

测试.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())

谁能看出我做错了什么?

最佳答案

此错误是由于没有为表单集的表单数据使用正确的前缀引起的。 Django 的 inlineformset_factory 自动将表单集前缀设置为在子模型的 ForeignKeyField 中定义的 related_name

模型.py:

class BingoCardSquare(models.Model):

...

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

在这种情况下,self.data 属性应该设置如下:

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

每个额外的表格都需要以相同的方式作为前缀。希望这个答案能帮助 future 陷入困境的 djangonaut。

关于python - Django Formset 缺少管理表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46817450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com