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...
📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀:
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:
• Django ORM Deep Dive
𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:
📖 Blog: https://bugbytes.io/posts/
👾 Github: https://github.com/bugbytes-io/django...
🐦 Twitter: / bugbytesio
📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:
Github: https://github.com/bugbytes-io/django...
Django filter() function: https://docs.djangoproject.com/en/4.2...
Django get() function: https://docs.djangoproject.com/en/4.2... Field Lookups: https://docs.djangoproject.com/en/4.2...
Django Lower function: https://docs.djangoproject.com/en/4.2...
#python #django #webdevelopment #database #sql