dan*_*ing 11 python django formset
我对formset有点问题.
我必须在页面中显示多个formset,每个formset都有几种形式.所以我做了类似的事情:
#GET
for prod in products:
ProductFormSet = modelformset_factory(Product,exclude=('date',),extra=prod.amount)
formsset.append(ProductFormSet(prefix="prod_%d"%prod.pk))
#POST
for prod in products:
ProductFormSet = modelformset_factory(Product,exclude=('date',),extra=prod.amount)
formsset.append(ProductFormSet(request.POST,prefix="prod_%d"%prod.pk))
Run Code Online (Sandbox Code Playgroud)
问题是,当我提交页面时,空帐表单"自动"有效(没有检查),但是如果我在一个表单中填写一个字段,则检查对其起作用.
我不知道为什么,所以如果有人有想法,
谢谢.
Jon*_*nas 22
我在研究另一个问题时遇到了这个问题.在挖掘Django源代码寻找我的问题的解决方案时,我找到了这个问题的答案,所以我将在这里记录:
当允许表单具有空值(这适用于表单集中包含的空表单)并且提交的值未从初始值更改时,将跳过验证.检查django/forms/forms.py中的full_clean()方法(Django 1.2中的第265行):
# If the form is permitted to be empty, and none of the form data has
# changed from the initial data, short circuit any validation.
if self.empty_permitted and not self.has_changed():
return
Run Code Online (Sandbox Code Playgroud)
我不确定你正在寻找什么样的解决方案(此外,这个问题已经有些过时了)但也许这将有助于未来的某些人.
Dan*_*man 11
@Jonas,谢谢.我用你的描述来解决我的问题.我需要一个表单,以便在空时验证.(用javascript添加的表单)
class FacilityForm(forms.ModelForm):
class Meta:
model = Facility
def __init__(self, *arg, **kwarg):
super(FacilityForm, self).__init__(*arg, **kwarg)
self.empty_permitted = False
facility_formset = modelformset_factory(Facility, form=FacilityForm)(request.POST)
Run Code Online (Sandbox Code Playgroud)
它将确保提交时任何显示的表单都不能为空.
归档时间: |
|
查看次数: |
11329 次 |
最近记录: |