0

我继承了一些由多个开发人员处理的代码,因此,产品没有标准。我遇到了严重的性能问题,并且正在解决很多问题,但不知道如何解决这个问题。当用户在预先填充数据并允许用户选择活动日期的页面上提交时,他们可以提交,并且任何带有日期的东西都会消失。问题出在views.py中。当用户提交时,以下代码运行,并在formset = formset(request.POST). 我假设发生了多个数据库调用,但是当我打印到终端时没有看到它们。下面是整个提交过程。

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":
    print 'if request.POST'
    print datetime.datetime.now()
    formset = formset(request.POST)
    print 'created formset'
    print datetime.datetime.now()
    if formset.is_valid():
        print 'formset valid'
        print datetime.datetime.now()
        updated_transactions = formset.save(commit=False)
        print 'updated transactions'
        print datetime.datetime.now()
        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))


Forms:

class StudyOnlyForm(forms.Form):
    action = forms.CharField(widget=forms.HiddenInput())
    #
    # JBL - Q4.0 changed this form to pull all studies (previously
    #       only 'active' studies), which really means all studies
    #       that are not 'ccc' studies
    #
    study = forms.ModelChoiceField(required=False,
            label='Protocol',
            queryset=Study.objects.all().exclude(study_status='ccc'))

    def __init__(self, *args, **kwargs):
        self.req = True
        if 'req' in kwargs:
            self.req = kwargs.pop('req')
        super(StudyOnlyForm, self).__init__(*args, **kwargs)
        print 'StudyOnlyForm() init'
        self.fields['study'].required = self.req

class PaidDateForm(forms.ModelForm):
    formfield_callback = jquery_datefield
    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):
        super(PaidDateForm, self).__init__(*args, **kwargs)
        print 'PaidDateForm init'
        for field in self.fields:
            if field != 'date_cleared':
                self.fields[field].widget = forms.HiddenInput()
        self.fields['paid_amount'].widget.attrs['size'] = 12
        self.initial['paid_amount'] = '%.2f' % (self.instance.usd_amount)
4

2 回答 2

1

您将查询集限制在 GET 分支中,而不是 POST 分支。这意味着 POST 分支会创建一个包含每个事务的表单集。

于 2013-06-18T19:29:51.183 回答
0

替换你的 for 循环:

for trans in updated_transactions.exclude(paid_amount=None, date_cleared=None).all():
    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()
于 2013-06-18T19:29:28.170 回答