Django proper way to model a one-to-one relationship

I'm looking for proper way to model such statements in Django:

 1. "A Condition has exactly 2 Blocks: leftBlock and rightBlock"
 2. "LeftBlock and RightBlock have the same fields in common"
 3. "Blocks must be different in a Condition"
 4. "A Block only belongs to 1 Condition"

Here my reasoning:

  • I identified 2 models: Condition and Block.
  • To express 1, Condition must have 2 fields named like "left_block" "right_block"
  • To express 2, it is enough one model called Block instead of 2 that are redundant.
  • As for 3, this may be done with DJango CheckConstraint but I'm not sure.
  • As for 4, I believe that we have one-to-one relationship between Condition and Block.

Finally, my models:

class Condition(models.Model):
    condition_id = models.UUIDField(
        primary_key=True, default=uuid.uuid4, editable=False, unique=True)
    left_block = models.OneToOneField(Block, on_delete=models.CASCADE, null=True, related_name='left_block')
    right_block = models.OneToOneField(Block, on_delete=models.CASCADE, null=True, related_name='right_block')

class Block(models.Model):
    block_id = models.UUIDField(
        primary_key=True, default=uuid.uuid4, editable=False, unique=True)
    name = models.CharField(max_length=16)

So, is this correct? If not, how can I handle such situation?

Back to Top