模型Formset验证错误

时间:2014-02-24 11:04:33

标签: django django-forms

我想使用带有自定义表单的模型formset。我使用不同的GET视图和POST函数的不同视图,因为我的html页面由三个不同的模型表单集组成。我的模特是一个

class Image(models.Model):
    customer = models.ForeignKey(Customer)
    doctor = models.ForeignKey(Doctor)
    date = models.DateField()
    desc = models.CharField(max_length=30)
    type = models.CharField(choices=TITLE,max_length=20)
    image = models.ImageField(upload_to='/pictures/', verbose_name='Image')



XRAY=[
    ('---------','---------'),
    ('PA Ceph', 'PA Ceph'),
    ('Lateral Ceph', 'Lateral Ceph'),
    ('Panoramic', 'Panoramic'),
]

class XrayImageForm(ModelForm):
    desc = forms.ChoiceField(choices=XRAY,required=True,  widget=Select(attrs={"class":"form-control input-sm"}))

    class Meta:
        model = Image
        exclude = ('customer', 'date','type', 'doctor',)
        widgets = {
            'desc':Select(attrs={'class':'form-control input-sm'}),
            'date': TextInput(attrs={'class':'form-control input-sm datepicker input-append date',
                               'readonly':''}),
    }


    def save(self, commit=True):
        model = super(XrayImageForm, self).save(commit=False)
        model.desc = self.cleaned_data['desc'];

        if commit:
            model.save()

        return model

class InternalImageForm(ModelForm):
   desc = form.ChoiceField(....) # I have to do this cause different ModelForm has different choices in the desc field

    class Meta:
        model = Image
        exclude = ('customer',)

我的获取视图如下

def images(request, customer_id):
    images = Image.objects.all().order_by('date')
    pictures = {}
    for image in images:
        date = image.date.stformat("%d/%M/%Y")
        if not pictures.has_key(date):
            pictures[date] = {image.title:[image,]}
        else:
            if pictures[date].has_key(image.title):
                pictures[date][image.title].append(image)
            else:
                pictures[date] = {image.title:[image,]}
    xray_formset = modelformset_factory(Image, form=XrayImageForm,extra=4)
    xray_formset(queryset=Image.objects.none())
    internal_form = InternalImageForm()
    external_form = ExternalImageForm()
    args = dict(pictures=pictures, xray_formset=xray_formset, internal_form=internal_form, external_form=external_form, customer_id=customer_id)

    return render_to_response('customer/images.html', args, context_instance=RequestContext(request))

我希望按日期过滤它们,每个日期按标题过滤(不同的图像可以具有相同的标题和相同的日期)

我的帖子

def upload_xray(request, customer_id):
    customer = Customer.objects.get(pk=customer_id)

    if request.method == 'POST':
        XrayFormSet = modelformset_factory(Image, form=XrayImageForm, extra=4)
        xray_formset = XrayFormSet(request.POST, request.FILES)
        print xray_formset

    return redirect('customer-images', customer_id=customer_id)

但是当我发布数据时,我得到了一个

ValidationError
Exception Value:[u'ManagementForm data is missing or has been tampered with']

我没有做任何实际的保存,只是想看看它是否有效。此外,所有字段都是必填项,但我没有填写页面上的formset中的所有字段(假设用户可以上传4张图片,但他可能不想这样做。)。希望我有点意义......为什么会出错?

1 个答案:

答案 0 :(得分:1)

确保您在模板中加入management form

{{ xray_formset.management_form }}

如果这不起作用,请更新您的问题以包含您的模板。