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