I am trying to optimize the following Django view.
def test_view(request, username):
msgs = MyModel.objects.filter(name=username,
created_at__range=[start_date, end_date]).order_by('-id')
arr = []
for msg in msgs:
c = TestModel.objects.get(id=msg.test_id)
if c not in arr:
arr.append(c)
return render(request, "test.html", {'context': arr})
So I have two models.
test_id
from MyModel
ordered by id
of MyModel
. MyModel
. They are test_id
and created_at
Currently I can see the following ways to improve.
I am relatively new to Django. So any help on how I should proceed or any links I should read would be appreciated.
I would suggest modifying your view as follows:
def test_view(request, username):
msgs = MyModel.objects.filter(
name=username, created_at__range=[start_date, end_date]
).order_by('-id').values_list('test_id', flat=True)
arr = TestModel.objects.filter(id__in=msgs)
return render(request, "test.html", {'context': arr})
That way you avoid just fetch all TestModel's you need in one query. Also, using values_list on MyModel query would allow to save memory since you don't use anything but test_id anyway.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.