Consultas de Campo e exclude()
Consulte com contains, gte, in e muito mais
Consultas de Campo e exclude() é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Django Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Django Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Beyond Equality
Plain filters check for equality, but real queries need ranges and text matches. Field lookups unlock that extra power.
The Double Underscore
A lookup is the field name, a double underscore, then the operator. That tiny syntax is how Django reads richer conditions.
Book.objects.filter(year__gte=2020)Greater and Less Than
Use gt, gte, lt, and lte for numeric or date ranges. They map straight to SQL comparison operators.
Book.objects.filter(price__lt=20)Search Inside Text
The contains lookup finds rows where a field holds your substring anywhere inside it. Great for simple searches.
Book.objects.filter(title__contains="Python")Ignore the Case
Add an i for case-insensitive text lookups like icontains or iexact, so capitals never trip up a match.
Book.objects.filter(title__icontains="python")Starts and Ends
Match the edges of a string with startswith and endswith. Both have case-insensitive twins too.
Book.objects.filter(title__startswith="The")Match a Set of Values
The in lookup keeps rows whose value appears in a list you pass. It is cleaner than chaining many OR checks.
Book.objects.filter(year__in=[2022, 2023, 2024])Check for Empty Values
Use isnull to find rows where a column is NULL, or set it False to find the rows that have a value.
Book.objects.filter(summary__isnull=True)Flip the Logic
Sometimes you want everything except a match. exclude() returns the rows that do NOT meet the condition.
Book.objects.exclude(published=True)Lookups Work in exclude Too
Any field lookup that works in filter also works in exclude(). The only difference is the result is inverted.
Book.objects.exclude(year__gte=2020)Combine filter and exclude
Chain both together to keep one group and drop another. Each call narrows the same lazy QuerySet further.
Book.objects.filter(published=True).exclude(price__gt=50)Quick Check
You want books whose title holds "django" regardless of capitalization. Which lookup fits?
Recap
You leveled up! Field lookups use the double underscore for ranges and text, and exclude() flips any condition. 🔍
Perguntas Frequentes
A aula “Consultas de Campo e exclude()” é grátis?
Sim — o texto completo de “Consultas de Campo e exclude()” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Django Academy, atualize para CoddyKit PRO. O curso de Django Academy inclui 4 aulas no total.
O que vou aprender em “Consultas de Campo e exclude()”?
Consulte com contains, gte, in e muito mais Você pratica Django Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Django Academy?
Nenhuma experiência prévia é necessária. Django Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “Consultas de Campo e exclude()”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Django Academy?
Sim. Cada aula de Django Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- all(), get() e filter()
- Consultas de Campo e exclude()
- order_by, Recortes e Avaliação Preguiçosa
- values, values_list e count