【发布时间】:2012-07-24 04:12:57
【问题描述】:
我已经阅读了大约 100 倍的 Forms 和 Formset Django 文档。为了清楚起见,这可能是我第一次使用 super() 或尝试从另一个类重载/继承(对我来说很重要。)
发生了什么事?我正在视图中制作 django-model-formset 并将其传递给模板。 formset 继承的模型恰好是 ManyToMany 关系。我希望这些关系是唯一的,因此如果我的用户正在创建一个表单并且他们不小心为 ManyToMany 选择了相同的对象,我希望它验证失败。
我相信我已经正确地编写了这个自定义“BaseModelFormSet”(通过文档),但我得到了一个 KeyError。它告诉我它找不到cleaned_data['tech'] 并且我在下面评论的行中的'tech' 一词上得到了KeyError。
模型:
class Tech_Onsite(models.Model):
tech = models.ForeignKey(User)
ticket = models.ForeignKey(Ticket)
in_time = models.DateTimeField(blank=False)
out_time = models.DateTimeField(blank=False)
def total_time(self):
return self.out_time - self.in_time
自定义的BaseModelFormSet:
from django.forms.models import BaseModelFormSet
from django.core.exceptions import ValidationError
class BaseTechOnsiteFormset(BaseModelFormSet):
def clean(self):
""" Checks to make sure there are unique techs present """
super(BaseTechOnsiteFormset, self).clean()
if any(self.errors):
# Don't bother validating enless the rest of the form is valid
return
techs_present = []
for form in self.forms:
tech = form.cleaned_data['tech'] ## KeyError: 'tech' <-
if tech in techs_present:
raise ValidationError("You cannot input multiple times for the same technician. Please make sure you did not select the same technician twice.")
techs_present.append(tech)
观点:(摘要)
## I am instantiating my view with POST data:
tech_onsite_form = tech_onsite_formset(request.POST, request.FILES)
## I am receiving an error when the script reaches:
if tech_onsite_form.is_valid():
## blah blah blah..
【问题讨论】:
-
为什么不直接将 form.cleaned_data 打印到日志输出中,看看有哪些键?你确定你的表单上有一个
tech字段,并且它被称为那个字段吗? -
@jozzas,我想这样做,但是将所有这些表单转换为字典以传递到表单并模拟所有这些过程将是一件很麻烦的事情。但是,我这样做了,发现我总共有 4 个表格,其中 2 个是空白的。当我遍历那些寻找“技术”的空白表单时,它引发了一个 KeyError。所以谢谢你鼓励我。
标签: python django django-models django-forms