Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Field Lookups | Complex Queries
Django ORM Ninja: Advanced Techniques for Developers

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';)
Book.objects.filter(genre__exact="philosophy")
  • iexact - case-insensitive match.
    (SELECT * FROM Author WHERE LOWER(pen_name) = LOWER('george orwell');)
Author.objects.filter(pen_name__iexact="george orwell")
  • contains - case-sensitive containment test.
    (SELECT * FROM Book WHERE title LIKE '%House%';)
Book.objects.filter(title__contains="House")
  • icontains - case-insensitive containment test.
    (SELECT * FROM Book WHERE LOWER(title) LIKE LOWER('%house%');)
Book.objects.filter(title__icontains="house")
  • in - in a given iterable; often used for filtering by a list of values.
    (SELECT * FROM Author WHERE id IN (1, 4, 7);)
Author.objects.filter(pk__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;)
Author.objects.filter(pk__lt=10)
  • startswith, istartswith - string starts with a value (case-sensitive and insensitive versions).
    (SELECT * FROM Author WHERE first_name LIKE 'J%';)
Author.objects.filter(first_name__startswith="J")
  • endswith, iendswith - string ends with a value (case-sensitive and insensitive versions).
    (SELECT * FROM Author WHERE last_name LIKE '%e';)
Author.objects.filter(last_name__endswith="e")
  • range - within a certain range (inclusive).
    (SELECT * FROM Book WHERE id BETWEEN 1 AND 5;)
Book.objects.filter(pk__range=(1, 5))
  • date, year, month, day - specific date components.
    (SELECT * FROM Book WHERE EXTRACT(YEAR FROM pub_date) = 2023;)
Book.objects.filter(pub_date__year=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?

question mark

What is the default field lookup type in Django?

Select the correct answer

question mark

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

Select the correct answer

question mark

What does the lookup 'pk__lt=10' signify?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 3.57

bookField Lookups

Svep för att visa menyn

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';)
Book.objects.filter(genre__exact="philosophy")
  • iexact - case-insensitive match.
    (SELECT * FROM Author WHERE LOWER(pen_name) = LOWER('george orwell');)
Author.objects.filter(pen_name__iexact="george orwell")
  • contains - case-sensitive containment test.
    (SELECT * FROM Book WHERE title LIKE '%House%';)
Book.objects.filter(title__contains="House")
  • icontains - case-insensitive containment test.
    (SELECT * FROM Book WHERE LOWER(title) LIKE LOWER('%house%');)
Book.objects.filter(title__icontains="house")
  • in - in a given iterable; often used for filtering by a list of values.
    (SELECT * FROM Author WHERE id IN (1, 4, 7);)
Author.objects.filter(pk__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;)
Author.objects.filter(pk__lt=10)
  • startswith, istartswith - string starts with a value (case-sensitive and insensitive versions).
    (SELECT * FROM Author WHERE first_name LIKE 'J%';)
Author.objects.filter(first_name__startswith="J")
  • endswith, iendswith - string ends with a value (case-sensitive and insensitive versions).
    (SELECT * FROM Author WHERE last_name LIKE '%e';)
Author.objects.filter(last_name__endswith="e")
  • range - within a certain range (inclusive).
    (SELECT * FROM Book WHERE id BETWEEN 1 AND 5;)
Book.objects.filter(pk__range=(1, 5))
  • date, year, month, day - specific date components.
    (SELECT * FROM Book WHERE EXTRACT(YEAR FROM pub_date) = 2023;)
Book.objects.filter(pub_date__year=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?

question mark

What is the default field lookup type in Django?

Select the correct answer

question mark

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

Select the correct answer

question mark

What does the lookup 'pk__lt=10' signify?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 1
some-alt