Django Models ************* - :doc:`dev-django-field` - :doc:`dev-django-migrations` ``UniqueConstraint`` ==================== Adapted from `How to have unique=True email addresses while allowing for null=true`_ :: from django.db import models from django.db.models import Q class Contact(TimedCreateModifyDeleteModel): # ... msgraph_user_uuid = models.UUIDField(null=True) class Meta: # ... # If we have a ``msgraph_user_uuid``, then it must be unique. constraints = [ models.UniqueConstraint( "msgraph_user_uuid", name="constraint_unique_msgraph_user_uuid_no_null", condition=Q(msgraph_user_uuid__isnull=False), ) ] .. _`How to have unique=True email addresses while allowing for null=true`: https://www.reddit.com/r/django/comments/14d7eqt/how_to_have_uniquetrue_email_addresses_while/