Conteúdo do Curso
Django ORM Ninja: Advanced Techniques for Developers
Django ORM Ninja: Advanced Techniques for Developers
Field Lookups
A field lookup is essentially a way to specify how Django should filter query results based on a model field's value.
Basic Structure
A field lookup takes the form field__lookuptype=value
. Here, field is the name of the model field, lookuptype is the type of comparison, and value is the value to compare against.
Common Field Lookups
exact
- the default lookup type; it performs case-sensitive comparisons.
(SELECT * FROM Book WHERE genre = 'philosophy';)
iexact
- case-insensitive match.
(SELECT * FROM Author WHERE LOWER(pen_name) = LOWER('george orwell');)
contains
- case-sensitive containment test.
(SELECT * FROM Book WHERE title LIKE '%House%';)
icontains
- case-insensitive containment test.
(SELECT * FROM Book WHERE LOWER(title) LIKE LOWER('%house%');)
in
- in a given iterable; often used for filtering by a list of values.
(SELECT * FROM Author WHERE id IN (1, 4, 7);)
gt
,gte
,lt
,lte
- greater than, greater than or equal to, less than, and less than or equal to.
(SELECT * FROM Author WHERE id < 10;)
startswith
,istartswith
- string starts with a value (case-sensitive and insensitive versions).
(SELECT * FROM Author WHERE first_name LIKE 'J%';)
endswith
,iendswith
- string ends with a value (case-sensitive and insensitive versions).
(SELECT * FROM Author WHERE last_name LIKE '%e';)
range
- within a certain range (inclusive).
(SELECT * FROM Book WHERE id BETWEEN 1 AND 5;)
date
,year
,month
,day
- specific date components.
(SELECT * FROM Book WHERE EXTRACT(YEAR FROM pub_date) = 2023;)
Obrigado pelo seu feedback!