10.4.3 Ensuring a Unique Identifier for NewsLink

In the previous section, we determined that our NewsLink objects must be unique according to their slug and startup fields. However, we are not currently enforcing uniqueness in Django or the database.

Luckily, Django makes it easy to enforce uniqueness according to multiple fields, thanks to the unique_together attribute of the nested Meta class, as shown in Example 10.36.

Example 10.36: Project Code

organizer/models.py in ead1b3cad1

70  class NewsLink(models.Model):  .      ... 77      class Meta:  .          ... 81          unique_together = (('slug', 'startup'),)

For convenience, instead of passing a tuple of tuples, Django allows us to pass a single tuple if ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.