title: Django - disable duplicates in related set
body: <p>There is a class <code>Order</code>, <code>SubOrder</code> and class <code>Product</code>. I want to make sure that no <code>Order</code> object will contain multiple <code>SubOrder</code> objects with the same <code>Product</code> object.</p>
  
  <pre><code>class Order(TimeStampedModel):
      ....
  
  class SubOrder(models.Model):
      order = models.ForeignKey('orders.Order', on_delete=models.CASCADE, related_name='suborders')
      product = models.ForeignKey('products.Product', on_delete=models.CASCADE)
      quantity = models.PositiveIntegerField(verbose_name='Počet')
  </code></pre>
  
  <p>So if there were 3 products - <strong>A, B, C</strong></p>
  
  <p>I don't want allow one <code>Order</code> object to have multiple <code>SubOrders</code> with the same <code>Product</code>. 
  For example <code>Order[Suborder[A,12],Suborder[B,12],Suborder[B,5]]</code> should raise <code>ValidationError</code> because there are two <code>SubOrders</code> with the same <code>Product</code> <strong>B</strong>.</p>
  
  <p>Is it possible to do that on the <code>model</code> or <code>database</code> layer?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried this:</p>
  
  <pre><code>def clean(self):
      neighbour_suborders = self.order.suborders.filter(product=self.product)
      if self.pk:
          neighbour_suborders = neighbour_suborders.exclude(pk=self.pk)
      if neighbour_suborders.exists():
          raise ValidationError("Takýto produkt už v objednávke existuje!")
  </code></pre>
  
  <p>But it allows to create such order in Django admin. </p>