0

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

虽然表单集中的每个表单都按预期提交了正确的数据,但cleaned_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}]
4

1 回答 1

0

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

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

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

于 2013-01-28T17:33:23.207 回答