COOKBOOK 4: Wichtige Field Lookups und Filter

Eine Übersicht über die wichtigsten Field Lookups mit der filter()-Methode.

contains

>>> qs = Category.objects.all()
>>> qs = qs.filter(description__contains='a')
>>> qs = qs.filter(name__icontains='Super')
>>>
>>> # Alle Kategorien, deren Event-Authoren ein  "Alfred" im Vornamen haben
>>> qs = Category.objects.filter(events__author__first_name__icontains='alfred')

isnull

>>> categories = Category.objects.filter(sub_title__isnull=True)

startswith

>>> categories = Category.objects.filter(name__startswith='J')
>>> categories = Category.objects.filter(category__name__startswith='Spo')
>>> events = Event.objects.filter(author__first_name__startswith='Bilbo')

endswith / iendswith

>>> categories = Category.objects.filter(name__endswith='urm')
>>> categories = Category.objects.filter(category__name__endswith='rt')
>>> events = Event.objects.filter(author__last_name__endswith='gins')

gt, gte, lt, lte

Greater than.

>>> categories = Category.objects.filter(num_of_events__gt=5)
>>> categories = Category.objects.filter(num_of_events__lt=5)
>>> categories = Category.objects.filter(num_of_events__lte=5)
>>> categories = Category.objects.filter(num_of_events__gte=5)

SQL Query des Querysets betrachten

>>> categories = Category.objects.filter(num_of_events__gt=5)
>>> categorie.query.__str__()