Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Field Lookups | Complex Queries
Django ORM Ninja: Técnicas Avanzadas para Desarrolladores
course content

Contenido del Curso

Django ORM Ninja: Técnicas Avanzadas para Desarrolladores

Django ORM Ninja: Técnicas Avanzadas para Desarrolladores

1. Introduction to Django ORM
2. Models, datatypes, and fields
3. Queries
4. Relations
5. Complex Queries
6. Advanced

bookField 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;)
1. What is the default field lookup type in Django?
2. How do you filter a queryset to include records where the field value starts with a certain string, case-sensitively?
3. What does the lookup 'pk__lt=10' signify?
What is the default field lookup type in Django?

What is the default field lookup type in Django?

Selecciona la respuesta correcta

How do you filter a queryset to include records where the field value starts with a certain string, case-sensitively?

How do you filter a queryset to include records where the field value starts with a certain string, case-sensitively?

Selecciona la respuesta correcta

What does the lookup 'pk__lt=10' signify?

What does the lookup 'pk__lt=10' signify?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 1
some-alt