Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django filter queryst after retrieving

I’m making a django website and due to the load on the database I’d like to filter the queryset after retrieving it.

files = Files.objects.get(folder_id=folder_id)

first_file = files.objects.filter(sequence = 0)

This example throws me error, same if I tried for loop. So is it possible to filter the retrieved queryset without interacting with database ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

when you run get that executes the query, returns a single Files object. https://docs.djangoproject.com/en/4.1/ref/models/querysets/#get

you would want to change the first line to filter (which won’t actually execute the query yet)
https://docs.djangoproject.com/en/4.1/ref/models/querysets/#filter

and the second line to get.

files = Files.objects.filter(folder_id=folder_id)

first_file = files.objects.get(sequence = 0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading