LingoRecentTransactionsCell: optimize query (#68212)
- use an union instead of an OR - use the fact that union is an implicit distinct to simplify the code - index the start_date field so it can be used to filter efficiently
This commit is contained in:
parent
a5fe6b569f
commit
76167cab03
|
@ -0,0 +1,16 @@
|
|||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('lingo', '0046_display_condition'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='Transaction',
|
||||
name='start_date',
|
||||
field=models.DateTimeField(auto_now_add=True, db_index=True),
|
||||
),
|
||||
]
|
|
@ -815,7 +815,7 @@ class Transaction(models.Model):
|
|||
items = models.ManyToManyField(BasketItem, blank=True)
|
||||
remote_items = models.CharField(max_length=512)
|
||||
to_be_paid_remote_items = models.CharField(max_length=512, null=True)
|
||||
start_date = models.DateTimeField(auto_now_add=True)
|
||||
start_date = models.DateTimeField(auto_now_add=True, db_index=True)
|
||||
end_date = models.DateTimeField(null=True)
|
||||
bank_data = JSONField(blank=True, default=dict)
|
||||
order_id = models.CharField(max_length=200)
|
||||
|
@ -1110,9 +1110,10 @@ class LingoRecentTransactionsCell(CellBase):
|
|||
# list transactions :
|
||||
# * paid by the user
|
||||
# * or linked to a BasketItem of the user
|
||||
qs = Transaction.objects.filter(models.Q(user=user) | models.Q(items__user=user)).filter(
|
||||
qs_recent_txn = Transaction.objects.filter(
|
||||
start_date__gte=timezone.now() - datetime.timedelta(days=7)
|
||||
)
|
||||
qs = qs_recent_txn.filter(user=user).union(qs_recent_txn.filter(items__user=user))
|
||||
if poll:
|
||||
for transaction in qs:
|
||||
if transaction.can_poll_backend() and transaction.is_running():
|
||||
|
@ -1128,9 +1129,7 @@ class LingoRecentTransactionsCell(CellBase):
|
|||
|
||||
def render(self, context):
|
||||
recent_transactions_template = template.loader.get_template('lingo/combo/recent_transactions.html')
|
||||
context['transactions'] = (
|
||||
self.get_transactions_queryset(context, poll=True).distinct().order_by('-start_date')
|
||||
)
|
||||
context['transactions'] = self.get_transactions_queryset(context, poll=True).order_by('-start_date')
|
||||
return recent_transactions_template.render(context)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue