MENU

Fun & Interesting

Django ORM - QuerySet Filtering and Lookups / Ordering and Slicing QuerySets

BugBytes 12,773 2 years ago
Video Not Working? Fix It Now

In this video, we'll dive into many topics, including how to filter QuerySets of records in Django, using the filter() and exclude() functions, and how to pass lookups such as "gt" (greater than), "lt" (less than), "range", "in", and "startswith". We will see how these lookups translate to the underlying SQL, for example: * "in" = IN operator in SQL * "range" = BETWEEN operator etc We will also look at how to order QuerySets in Django, using the order_by() function, and using the model Meta class to define a default ordering. We'll also see how to apply the Lower database function in order to retrieve a case-insensitive ordering over character fields. We'll see how to index/slice into QuerySets, how to use the get() function to retrieve a model, the exists() function to check for the existence of records in a QuerySet, and the earliest() and latest() functions to retrieve the earliest and latest objects using a date/datetime field. Starter Code: https://github.com/bugbytes-io/django-orm-series ๐Ÿ“Œ ๐—–๐—ต๐—ฎ๐—ฝ๐˜๐—ฒ๐—ฟ๐˜€: 00:00 Intro 01:09 Setup and management command 04:51 Filtering QuerySets with filter() method 07:39 Getting a single model back with the get() method 10:03 Checking if QuerySet contains records with exists() method 10:38 Multiple AND conditions with the filter() method 13:24 Filtering QuerySets with the โ€œinโ€ lookup 15:17 Filtering QuerySets with the exclude() method 17:52 Filtering QuerySets with โ€œltโ€ and โ€œgtโ€ lookups 21:39 Filtering QuerySets with the range lookup 22:52 Ordering QuerySets with the order_by() method 26:15 Case-insensitive orderings with the Lower database function 28:47 Ordering by date & datetime fields 29:25 Indexing and Slicing into QuerySets - LIMIT and OFFSET SQL statements 31:26 Adding โ€˜orderingโ€™ field to model Meta class for default ordering 33:23 ORM earliest() and latest() functions 35:01 Meta class get_latest_by field 35:55 Filtering by foreign key values in Django โ˜•๏ธ ๐—•๐˜‚๐˜† ๐—บ๐—ฒ ๐—ฎ ๐—ฐ๐—ผ๐—ณ๐—ณ๐—ฒ๐—ฒ: To support the channel and encourage new videos, please consider buying me a coffee here: https://ko-fi.com/bugbytes โ–ถ๏ธ Full Playlist: https://www.youtube.com/playlist?list=PL-2EBeDYMIbQXKsyNweppuFptuogJe2L- ๐—ฆ๐—ผ๐—ฐ๐—ถ๐—ฎ๐—น ๐— ๐—ฒ๐—ฑ๐—ถ๐—ฎ: ๐Ÿ“– Blog: https://bugbytes.io/posts/ ๐Ÿ‘พ Github: https://github.com/bugbytes-io/django-orm-series ๐Ÿฆ Twitter: https://twitter.com/bugbytesio ๐Ÿ“š ๐—™๐˜‚๐—ฟ๐˜๐—ต๐—ฒ๐—ฟ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐—ฎ๐—ป๐—ฑ ๐—ถ๐—ป๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐˜๐—ถ๐—ผ๐—ป: Github: https://github.com/bugbytes-io/django-orm-series Django filter() function: https://docs.djangoproject.com/en/4.2/ref/models/querysets/#filter Django get() function: https://docs.djangoproject.com/en/4.2/ref/models/querysets/#getDjango Field Lookups: https://docs.djangoproject.com/en/4.2/ref/models/querysets/#field-lookups Django Lower function: https://docs.djangoproject.com/en/4.2/ref/models/database-functions/#lower #python #django #webdevelopment #database #sql

Comment