2013-01-25 83 views
0

我正在使用inlineformset_factory創建一個formset。父對象是特色項集和子項目對象是特色項目。我在前端使用'django-dynamic-formset'jQuery插件來動態添加/刪除formset表單。Django Formset不清理/正確保存

雖然formset中的每個表單都按照預期的方式獲得了正確的數據,但其中的清單數據只包含表單的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}] 

回答

0

我想通了。這條線在我的形式構造是(有時只?)拋出的IOError和搞砸了我的表單集表單驗證/清洗:

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

當我貼我的代碼,我拿出一些(我認爲是多餘的)形成代碼,但我拿出的結果卻是問題的根源。