Filter Latest Record In Django
Writing my first Django app that gets messages from other applications and stores reports about them. It is performing very slow due to the following logic that I hope can be impro
Solution 1:
If you're not afraid of executing a query for every app in your database you can try it this way:
def get_latest_report():
""" Returns the latest report from each app """
return [app.report_set.latest('date') for app in App.objects.all()]
This adds a query for every app in your database, but is really expressive and sometimes maintainability and readability are more important than performance.
Solution 2:
If you are using PostgreSQL you can use distinct
and order_by
in combination, giving you the latest report for each app like so
Report.objects.order_by('-date').distinct('app')
If you are using a database that does not support the DISTINCT ON
clause, MySQL for example, and you do not mind changing the default ordering of the Report
model, you can use prefetch_related to reduce 500+ queries to 2 (however this method will use a lot more memory as it will load every report)
class Report(models.Model):
# Fields
class Meta:
ordering = ['-date']
def get_latest_report():
latest_reports = []
for app in App.objects.all().prefetch_related('report_set'):
try:
latest_reports.append(app.report_set.all()[0])
except IndexError:
pass
return latest_reports
Post a Comment for "Filter Latest Record In Django"