March 2020
Intermediate to advanced
608 pages
17h 17m
English
We define two constraints in the database.
The first one, UniqueConstraint, tells the titles to be unique for each author. If the author is not set, the titles can be repeated. To check if the author is set we use the negated lookup: ~models.Q(author=None). Note that in Django, the ~ operator for lookups is equivalent to the exclude() method of a QuerySet, so these QuerySets are equivalent:
ideas_with_authors = Idea.objects.exclude(author=None)ideas_with_authors2 = Idea.objects.filter(~models.Q(author=None))
The second constraint, CheckConstraint, checks if the title doesn't start and end with a whitespace. For that, we use a regular expression lookup.