-2
>>> Entry.objects.filter(
...     headline__startswith='What'
... ).exclude(
...     pub_date__gte=datetime.now()
... ).filter(
...     pub_date__gte=datetime(2005, 1, 1)
... )

"This takes the initial QuerySet of all entries in the database, adds a filter, then an exclusion, then another filter. The final result is a QuerySet containing all entries with a headline that starts with “What”, that were published between January 1, 2005, and the current day."

Can someone explain what is the __ and the word gte doing in this context. I cannot visualize how the above query extracts the range between January 1, 2005, and the current day since we are excluding datetime.now . I am sure it is relating to something that I am missing regarding the double underscore and gte. Thanks.

BluePython
  • 1,635
  • 3
  • 24
  • 35
  • 1
    `pub_date__gte` == "Publication Date greater than or equal". Doesn't seem to make much sense to exclude posts from the future though... – l4mpi Sep 09 '13 at 21:12

1 Answers1

7

__gte just means "Greater than or equal" operator, this is just a special syntax for making WHERE clauses. There are others, like __gt, __lt, __lte, __startswith etc. See more info in documentation.

exclude() is just an opposite to filter, quote from docs:

filter(**kwargs)

Returns a new QuerySet containing objects that match the given lookup parameters.

exclude(**kwargs)

Returns a new QuerySet containing objects that do not match the given lookup parameters.

So, the code you've provided is actually saying:

give me all entries, where:

  • headline starts with What and
  • pub_date is not greater than or equal to current date and
  • pub_date is greater than or equal to 1/1/2005

Also see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195