列表索引超出范围错误让我好斗

时间:2013-06-20 13:57:50

标签: python django forms indexing

这对我来说已经好转了2天。由于性能不佳,我需要限制POST数据的大小。当我运行它时,我得到IndexError'列表索引超出范围'。我有一组复杂的查询运行,它们被捆绑在一起,结果被分配给'qs'。当调用formset工厂添加第二个表单时,它使用我的'Transaction'类对象;问题是,总的来说,这个表是巨大的。然后我创建了表单并将其与页面中的POST数据一起使用。收集数据时,好像是在查询整个表格。我附上了整个视图和有问题的表格。我感谢任何帮助。

查看:

def update_pdate(request):

    STATES = ['Routed','Reconciled']

    formset = None

    form = StudyOnlyForm()

    study_pk = request.GET.get('study', '0')
    if study_pk == '' or study_pk == '0':
        study_pk = 0

    # child transactions that are 'Recoginzed',
    # reportable with a 'Routed' parent
    qs = Transaction.objects.filter(contract__reportable=True,
            cleared = False,
            contract__study__pk=study_pk,
            transaction_status='Recognized',
            parent_transaction__isnull=False,
            parent_transaction__transaction_status__in=STATES)

    #Designed to capture standalone contracts
    qs1 = Transaction.objects.filter(contract__reportable=True,
            cleared = False,
            contract__study__pk=study_pk,
            contract__contract_type__name='Standalone',
            transaction_status__in=STATES)

    #Captures Arms contracts for Milestone payments
    parent_list = []
    arms_list = []

    parent = Transaction.objects.filter(parent_transaction__isnull=True,
        contract__reportable=True,
        cleared = False,
        contract__study__pk=study_pk,
        contract__contract_type__name='ARMs',
        transaction_status__in=STATES)

    children = Transaction.objects.filter(contract__reportable=True,
            cleared = False,
            contract__study__pk=study_pk,
            transaction_status='Recognized',
            contract__contract_type__name='ARMs',
            parent_transaction__isnull=False,
            parent_transaction__transaction_status__in=STATES)

    for child_item in children:
        parent_list.append(child_item.parent_transaction.pk)
        arms_list.append(child_item.pk)

    for parent_item in parent:
        if parent_item.pk not in parent_list:
            arms_list.append(parent_item.pk)

    qs3 = Transaction.objects.filter(pk__in=arms_list)

    qs4 = qs | qs1 | qs3

    qs = qs4.order_by('-pk')

    formset = modelformset_factory(Transaction, form=PaidDateForm, extra=0, can_delete=False)

    if request.method == "POST":
        formset = formset(request.POST, queryset = qs)
        if formset.is_valid():
            updated_transactions = formset.save(commit=False)
            for trans in updated_transactions:
                if trans.paid_amount is not None and trans.date_cleared is not None:
                    trans_to_change = Transaction.objects.get(pk=trans.pk)
                    trans_to_change.paid_amount = trans.paid_amount
                    trans_to_change.date_cleared = trans.date_cleared
                    trans_to_change.paid_currency = trans_to_change.entered_currency
                    trans_to_change.paid_amount_usd = Decimal(str(trans_to_change.paid_amount * Decimal(str(trans_to_change.exchange_rate)).quantize(Decimal('0.01')))).quantize(Decimal('0.01'))
                    trans_to_change.edited_by = request.user
                    trans_to_change.cleared = True
                    trans_to_change.save()
            if updated_transactions:
                messages.add_message(request, messages.INFO, 'The transactions have been updated successfully.')
                return HttpResponseRedirect(reverse('track:update_pdate'))
            else:
                messages.add_message(request, messages.INFO, 'No transactions have been updated.')

                return render_to_response(
                    'track/paid_date_update.html',
                    {'formset':formset,
                     'form': form,
                     'study_pk':study_pk,
                    },
                    context_instance=template.RequestContext(request))

    else:
        formset = formset(queryset=qs)

    return render_to_response(
            'track/paid_date_update.html',
            {'formset':formset,
             'form': form,
             'study_pk':study_pk,
            },
            context_instance=template.RequestContext(request))

形式:

class PaidDateForm(forms.ModelForm):
    print 'PaidDateForm() begins'
    formfield_callback = jquery_datefield
    print 'end of jquery callback'
    #date_paid = forms.DateField(label="Paid Date", required=False)
    #paid_amount = forms.DecimalField(label="Cleared Amount",max_digits=14,decimal_places=2,required=False)
    date_cleared = forms.DateField(label="Cleared Date",widget=JQueryDateWidget(), input_formats=settings.DATE_INPUT_FORMATS, required=False)

    class Meta:
        model = Transaction
        include = ('date_time_created')

    def __init__(self, *args, **kwargs):
        self.queryset = Transaction.objects.filter(pk__in=qs)
        super(PaidDateForm, self).__init__(*args, **kwargs)
        for field in self.fields:
            if field != 'date_cleared':
                #self.fields[field].queryset = qs
                self.fields[field].widget = forms.HiddenInput()
        #self.fields['paid_amount'].widget.attrs['size'] = 12
        #self.initial['paid_amount'] = '%.2f' % (self.instance.usd_amount)

1 个答案:

答案 0 :(得分:1)

您应该启用调试模式来研究错误报告。它具有完整的异常信息和追溯功能,可以在故障发生时实际显示大部分数据。我发现此信息非常有用。