title: Django UniqueConstraint
body: <h2>Context</h2>
  
  <p>I have the models <code>AppVersion</code>, <code>App</code> &amp; <code>DeployApp</code>. In the <code>AppVersion</code>  model users can upload APK files to the filesystem. I am using a <code>pre_save</code> signal to prevent uploading APK files with the same <code>version_code</code> for a specific <code>App</code> like this:</p>
  
  <pre><code>@receiver(pre_save, sender=AppVersion)
  def prevent_duplicate_version_code(sender, instance, **kwargs):
      qs = AppVersion.objects.filter(app_uuid=instance.app_uuid, version_code=instance.version_code)
      if qs.exists():
          raise FileExistsError("Version code has to be unique for a specific app")
  </code></pre>
  
  <p>This signal does what I want, except it also raises the error when I am trying to create an object in the bridge-table <code>DeployApp</code>.</p>
  
  <h2>Models</h2>
  
  <pre><code># models.py
  
  class App(models.Model):
      app_uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
      app_name = models.CharField(max_length=100)
  
  
  class AppVersion(models.Model):
      app_version_uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
      app_uuid = models.ForeignKey(App, on_delete=models.CASCADE, related_name='app_versions')
      app_version_name = models.CharField(max_length=100)
      version_code = models.IntegerField(blank=True, null=True, editable=False)
      source = models.FileField(upload_to=get_app_path, storage=AppVersionSystemStorage()) 
  
  
  class DeployApp(models.Model):
      deploy_app_uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
      app_version = models.ForeignKey(AppVersion, on_delete=models.CASCADE)
      device_group = models.ForeignKey(DeviceGroup, on_delete=models.CASCADE)
      release_date = UnixDateTimeField()
  </code></pre>
  
  <p>My guess is that when creating an object of <code>DeployApp</code> the related <code>AppVersion</code> is also saved and thus the <code>pre_save</code> signal is called and raises the Exception.</p>
  
  <p>I also tried to override the <code>save()</code> method for the <code>AppVersion</code> model but the results are the same.</p>
  
  <p>How do I make sure that the Exception only happens upon creating a new <code>AppVersion</code> instance and does not happen when adding or editing a <code>DeployApp</code> instance?</p>