Django Formset没有正确清理/保存

时间:2013-01-25 14:29:57

标签: django django-forms formset

我正在使用inlineformset_factory来创建一个formset。父对象是特色集,子对象是特色项。我正在使用前端的'django-dynamic-formset'jQuery插件来动态添加/删除formset表单。

虽然formset中的每个表单都按预期提交了正确的数据,但cleaning_data只包含表单的id。

查看:

@login_required
@csrf_protect
def edit_featured_set(request, fset_id):

    '''
    Edit a featured set and its items
    '''

    c = {}
    c.update(csrf(request))

    # get existing featured set for this id (if exists)
    if fset_id and int(fset_id) > 0:
        try:
            fset_obj = Featureditemset.objects.get(id=fset_id)
        except Featureditemset.DoesNotExist:
            # bad or no id passed
            return HttpResponse('ObjectDoesNotExist')
    else:
        # get new featured set object
        fset_obj = Featureditemset(id = get_next_auto_inc(Featureditemset))

    # define formset to work with featured items            
    FeaturedItemFormSet = inlineformset_factory(Featureditemset, Featureditem, extra=1, form=FeatureditemEditForm)

    if request.method == 'POST':

        form = FeatureditemsetEditForm(request.POST, request.FILES, instance=fset_obj, prefix='set')

        if form.is_valid():
            featured_item_formset = FeaturedItemFormSet(request.POST, request.FILES, instance=fset_obj, prefix='item')
            if featured_item_formset.is_valid():

                # save form
                form.save()

                # save formset
                print 'Formset: %s' % str(featured_item_formset)
                print 'cleaned_data: %s' % featured_item_formset.cleaned_data
                featured_item_formset.save()

                return HttpResponse('valid')                  
    else:
        form = FeatureditemsetEditForm(instance=fset_obj, prefix='set')

    featured_item_formset = FeaturedItemFormSet(instance=fset_obj, prefix='item')   
    c['form'] = form
    c['formset'] = featured_item_formset

    return render_to_response('admin/edit_featured.html', c, context_instance = RequestContext(request))

表单:

class FeatureditemEditForm(ModelForm):        
    class Meta:
        model = Featureditem
        fields = ('id','img', 'name', 'folio')

class FeatureditemsetEditForm(ModelForm):        
    class Meta:
        model = Featureditemset
        fields = ('name', 'application')

型号:

class Featureditemset(models.Model):

    id = models.BigIntegerField(primary_key=True, db_column='FEATUREDITEMSET_ID')    
    application = models.ForeignKey(Application, null=True, db_column='FEATUREDITEMSET_APPLICATION_ID', blank=True)    
    htmlcontent = models.TextField(db_column='FEATUREDITEMSET_HTMLCONTENT', blank=True)    
    modified = models.DateTimeField(db_column='FEATUREDITEMSET_MODIFIED', auto_now=True)    
    viewer = models.ForeignKey(Viewer, null=True, db_column='FEATUREDITEMSET_VIEWER', blank=True)    
    name = models.CharField(max_length=192, db_column='FEATUREDITEMSET_NAME', blank=True)

    class Meta:    
        db_table = u'featureditemset'

    def __unicode__(self):
        return str(self.name)

class Featureditem(models.Model):

    id = models.BigIntegerField(db_column='FEATURED_ITEM_ID', primary_key=True)
    img = models.FileField(upload_to='featured', db_column='FEATURED_ITEM_IMG_URL', blank=True)
    alt_title = models.CharField(max_length=768, db_column='FEATURED_ITEM_ALT_TITLE', blank=True)
    modified = models.DateTimeField(db_column='FEATURED_ITEM_MODIFIED', auto_now=True)
    folio = models.ForeignKey(Folio, db_column='FEATURED_ITEM_FOLIO_ID', blank=False)
    app = models.ForeignKey(Application, null=True, db_column='FEATURED_ITEM_APP_ID', blank=True)
    name = models.CharField(max_length=192, db_column='FEATURED_ITEM_NAME', blank=True)    
    featureditemset = models.ForeignKey(Featureditemset, null=True, db_column='FEATUREDITEM_FEATUREDITEMSET_ID', blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = u'featureditem'

输出到控制台:

Formset: <input type="hidden" name="item-TOTAL_FORMS" value="1" id="id_item-TOTAL_FORMS" /><input type="hidden" name="item-INITIAL_FORMS" value="1" id="id_item-INITIAL_FORMS" /><input type="hidden" name="item-MAX_NUM_FORMS" id="id_item-MAX_NUM_FORMS" />
<tr><th><label for="id_item-0-id">Id:</label></th><td><input type="text" name="item-0-id" value="5" id="id_item-0-id" /></td></tr>
<tr><th><label for="id_item-0-img">Featured Image:</label></th><td><input type="file" name="item-0-img" id="id_item-0-img" /></td></tr>
<tr><th><label for="id_item-0-name">Name:</label></th><td><input id="id_item-0-name" type="text" name="item-0-name" value="item1" maxlength="192" /></td></tr>
<tr><th><label for="id_item-0-folio">Folio:</label></th><td><select name="item-0-folio" id="id_item-0-folio">
<option value="1" selected="selected">0000054220110900000025</option>
<option value="2">com.maned.kwmultitest</option>
</select></td></tr>
<tr><th><label for="id_item-0-DELETE">Delete:</label></th><td><input type="checkbox" name="item-0-DELETE" id="id_item-0-DELETE" /><input type="hidden" name="item-0-featureditemset" id="id_item-0-featureditemset" /></td></tr>
cleaned_data: [{'id': 5}]

1 个答案:

答案 0 :(得分:0)

我明白了。我的表单构造函数中的这一行(有时只是?)抛出一个IOError并搞砸了我的formset表单验证/清理:

self.fields['img'] = forms.ImageField(label='Featured Image') # gets rid of 'clear' checkbox

当我发布我的代码时,我拿出了一些(我认为多余的)表单代码,但我拿出来的结果却是问题的根源。