简体   繁体   中英

Improving the Django Query

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.

  • MyModel
  • TestModel

Goal:

  • Fetch only unique values on a column called test_id from MyModel ordered by id of MyModel .
  • Need only two parameters from MyModel . They are test_id and created_at

Currently I can see the following ways to improve.

  • Use values in filter query to get only test_id and created_at.
  • Use the mysql Distinct keyword in Django to uniquely search on test_id
  • Unnecessary looping through the data queryset.
  • Again searching for collection in arr seems really unnecessary and time consuming.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM