I have the following model:
class Message(Model):
url = URLField("URL")
email = EmailField("E-Mail")
contacted = BooleanField("Contacted", default=False)
With example data like:
| url | email | contacted |
+-----+-----------------+-----------+
| foo | foo@example.com | N |
| bar | bar@example.com | N |
| baz | foo@example.com | Y |
I would like to select all distinct rows (by e-mail address) whose e-mail addresses have never been contacted. With this example data, the bar@example.com row would be the only one returned.
This will return the records you want:
This has the advantage of only running one query. Your query will look something like this:
Note that for many, many records this query may not be optimal, but it will probably work for most uses.