6.2. ORM QuerySet

  • Lazy evaluated

  • Combine queries

  • Object creation

  • Write ForeignKey on object save()

>>> Model.save()
>>> Model.objects.all()
>>> Model.objects.get()
>>> Model.objects.filter()
>>> Model.objects.exclude()
>>> Model.objects.distinct()
>>> Model.objects.get().query
>>> Model.objects.all()[:5]
>>> Model.objects.all()[5:10]
>>> Model.objects.order_by('publish_date')[0]

6.2.1. Filtered QuerySets are unique

>>> q1 = Entry.objects.filter(headline__startswith="What")
>>> q2 = q1.exclude(pub_date__gte=datetime.date.today())
>>> q3 = q1.filter(pub_date__gte=datetime.date.today())

6.2.2. QuerySets are lazy

>>> q = Entry.objects.filter(headline__startswith="What")
>>> q = q.filter(pub_date__lte=datetime.date.today())
>>> q = q.exclude(body_text__icontains="food")
>>> print(q)