0

inlineformset_factoryを使用してフォームセットを作成しています。親オブジェクトは注目のセットであり、子オブジェクトは注目のアイテムです。フロントエンドで「django-dynamic-formset」jQueryプラグインを使用して、フォームセットフォームを動的に追加/削除しています。

フォームセット内の各フォームは期待どおりに適切なデータで送信されますが、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 に答える