title: How to validate uniqueness constraint across foreign key (django)
body: <p>I have the following (simplified) data structure:</p>
  
  <pre><code>Site
  -&gt; Zone
     -&gt; Room
        -&gt; name
  </code></pre>
  
  <p>I want the name of each Room to be unique for each Site.</p>
  
  <p>I know that if I just wanted uniqueness for each Zone, I could do:</p>
  
  <pre><code>class Room(models.Model):
      zone = models.ForeignKey(Zone)
      name = models.CharField(max_length=255) 
  
      class Meta:
          unique_together = ('name', 'zone')
  </code></pre>
  
  <p>But I can't do what I really want, which is:</p>
  
  <pre><code>class Room(models.Model):
      zone = models.ForeignKey(Zone)
      name = models.CharField(max_length=255) 
  
      class Meta:
          unique_together = ('name', 'zone__site')
  </code></pre>
  
  <p>I tried adding a validate_unique method, as suggested by <a href="https://stackoverflow.com/questions/4440010/django-unique-together-with-foreign-keys">this question</a>:</p>
  
  <pre><code>class Room(models.Model):
      zone = models.ForeignKey(Zone)
      name = models.CharField(max_length=255) 
  
      def validate_unique(self, exclude=None):
          qs = Room.objects.filter(name=self.name)
          if qs.filter(zone__site=self.zone__site).exists():
              raise ValidationError('Name must be unique per site')
  
          models.Model.validate_unique(self, exclude=exclude)
  </code></pre>
  
  <p>but I must be misunderstanding the point/implementation of validate_unique, because it is not being called when I save a Room object.</p>
  
  <p>What would be the correct way to implement this check?</p>