title: Django model ON DELETE CASCADE policy, emulate ON DELETE RESTRICT instead, general solution
body: <p>I'd like to delete an instance of a model, but only if it doesn't have any other instance of another class with a foreign key pointing to it. From Django documentation:</p>
  
  <blockquote>
    <p>When Django deletes an object, it emulates the behavior of the SQL constraint ON DELETE CASCADE -- in other words, any objects which had foreign keys pointing at the object to be deleted will be deleted along with it.</p>
  </blockquote>
  
  <p>In a given example:</p>
  
  <pre><code>class TestA(models.Model)
      name = models.CharField()
  
  class TestB(models.Model)
      name = models.CharField()
      TestAs = models.ManyToManyField(TestA)
  
  # More classes with a ManyToMany relationship with TestA
  # ........
  </code></pre>
  
  <p>I'd like something like:</p>
  
  <pre><code>tA = TestA(name="testA1")
  tB = TestB(name="testB1")
  tB.testAs.add(tA)
  
  t = TestA.objects.get(name="testA1")
  
  if is_not_foreignkey(t):
      t.delete()
  else:
      print "Error, some instance is using this"
  </code></pre>
  
  <p>Should print the error. I know I can check for specific instances the foreignkey sets, like in this case check t.TestB_set(), but I am looking for a more general solution for any given model.</p>