[Answer]-Django form efficiency issue

1👍

You restrict the queryset in the GET branch, but not the POST branch. That means that the POST branch creates a formset containing every transaction.

0👍

replace your for loop:

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()

Leave a comment