django:modelformset_factory保存返回空列表

时间:2016-09-29 04:18:33

标签: python django

我是django的新手,我有这个代码,我不知道它是什么问题,我用引导程序编写django,直到我在form.py中包含小部件和模板中的样式:(

class QuotesView(View):
    def post(self, request, *args, **kwargs):
        qModFac = modelformset_factory(Quote, fields=('SerNr','SalesMan','Status'),extra=0)
        fieldsModel = ('Stock','Currency','Price','FinalPrice','Total','Comment','DeliveryDate','Suggested')
        query = Quote.objects.filter(somefilters).distinct()
        paginator = Paginator(query, 10)
        try: 
            objects = paginator.page(pageNumber)
        except PageNotAnInteger:
            objects = paginator.page(1)
        except EmptyPage: 
            objects = paginator.page(paginator.num_pages)
        formSet = qModFac(queryset=query)
        widgets = {'Stock':NumberInput(attrs=numAttrs),'Price':NumberInput(attrs=numAttrs),'FinalPrice':NumberInput(attrs=numAttrs),'Total':NumberInput(attrs=numAttrs),'Currency':Select(attrs=textAttrs),'Comment':TextInput(attrs=textAttrs),'Suggested':TextInput(attrs=textAttrs)}
        qduModFac = modelformset_factory(QuoteDetailUser,form=QuoteDetailUserForm, fields=fieldsModel,extra=0,widgets=widgets)
        #some debug
        #if i print request.POST i can see the values from html
        #it's like a u'form-0-DeliveryDate': [u'14/09/2016', u''],
        formDetailUserSet = qduModFac(request.POST)
        # I think qduModFac(request.POST) is the problem, but i don't know how do this manually
        if formDetailUserSet.is_valid():#Thats return TRUE!!!!
        #some debug
        #instances = formDetailUserSet.save() return empty list
        for rec in instances:
            res = rec.save()
            #res  is an object but, not updated :(
        return render(request,'supplierweb/quotes.html',{'formset':formSet,'formDetailUserSet':formDetailUserSet,
                                                             'QuoteStatus':status,'brand':brand,'objects':objects})

form.py

class QuoteDetailUserForm(forms.ModelForm):
    DATEPICKER = {
        'type': 'text',
        'class': 'datepicker inputText',
    }

    numAttrs = {'class' : 'inputQty'}
    textAttrs = {'class' : 'inputText','type':'text'}
    labelAttrs = {'class' : 'labelText cellMatrix','type':'text'}
    ArtCode = forms.CharField(max_length=20,required=False)
    Brand = forms.CharField(max_length=20,required=False)
    AlternativeCode = forms.CharField(max_length=20,required=False)
    Description = forms.CharField(max_length=100,required=False)
    Qty = forms.IntegerField(required=False)
    Stock = forms.IntegerField(required=False,widget=forms.NumberInput(attrs=numAttrs))
    MY_CHOICES = (
    ('USD', 'Dólares'),
    ('PEN', 'Soles'),
    )
    Currency = forms.ChoiceField(choices=MY_CHOICES,widget=forms.Select(attrs=textAttrs))
    Price = forms.FloatField(help_text='Ingrese precio unitario',required=False,widget=forms.NumberInput(attrs=numAttrs))
    FinalPrice = forms.FloatField(required=False,widget=forms.NumberInput(attrs=numAttrs))
    Total = forms.FloatField(required=False,widget=forms.NumberInput(attrs=numAttrs))
    Comment = forms.CharField(max_length=100,required=False,widget=forms.TextInput(attrs=textAttrs))
    DeliveryDate = forms.DateField(widget=forms.DateInput(attrs=DATEPICKER),required=False,input_formats=['%d/%m/%Y'])#(years=YEAR_CHOICES)
    Suggested = forms.CharField(max_length=20,required=False,widget=forms.TextInput(attrs=textAttrs))
    CreateDate = forms.DateField(required=False)#(years=YEAR_CHOICES)

    class Meta:
        model = QuoteDetailUser
        exclude = ('quote','user')

和模板或其中的一部分

    {{ formDetailUserSet.management_form }}
    {% for qDetail in formDetailUserSet %}
        <div class="row textMatrix " >
        {% for hidden in qDetail.hidden_fields %}
                {{ hidden }}
        {% endfor %} 
                <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" >
                    <p class="labelText cellMatrix"> {{ qDetail.instance.AlternativeCode }} </p>
                </div>
                <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 colDescription" >
                    <p class="labelText"> {{ qDetail.instance.Description }} </p>
                </div>
                <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 colBrand" >
                    <p class="labelText"> {{ qDetail.instance.Brand }} </p>
                </div>
                <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 colQty labelQty" id='qty-{{ forloop.counter }}' >
                     {{ qDetail.instance.Qty }}
                </div>
                <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 " >
                {{ qDetail.Stock }}
                </div>
                <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 " >
                    {{ qDetail.Currency }}
                </div>
                <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 " >
                    <p onchange='onAfterUpdate( "{{qDetail.Price.id_for_label }}" , "{{ forloop.counter }}" , "{{ qDetail.Total.id_for_label }}" );'>
                        {{ qDetail.Price }}
                    </p>
                </div>
                <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 " >
                    <p onchange='onAfterUpdate( "{{ qDetail.FinalPrice.id_for_label }}" , "{{ forloop.counter }}" , "{{ qDetail.Total.id_for_label }}" );'>
                        {{ qDetail.FinalPrice }}
                    </p>
                </div>

谢谢!

0 个答案:

没有答案