title: How to check if an instance of a model already exist in database
body: <p>So I defined a model <code>Language</code>. I need to track whoever created the language object. I also don't need multiple instances of the same language. For that reason I want to be able to check if a language already exists when a user tries to create one. If it exists then I'll just tell the user they can use the one already present. If not they can create it. I am using a custom <code>User</code> model.</p>
  
  <pre><code>USERS = get_user_model().objects.all()
  from random import choice
  
  class Language(models.Model):
      language = models.CharField(max_length=25, unique=True)
      creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  
      def __str__(self):
          return self.language
  
      def get_absolute_url(self):
          return reverse('chinemerem:language_index')
  
      def save(self, *args, **kwargs):
          self.language = self.language.upper()
          super(Language, self).save(*args, **kwargs)
  </code></pre>
  
  <p>I can create <code>Language</code> instances with <code>try/except</code> in <code>django</code> <code>shell</code> like below.</p>
  
  <pre><code>try:
      language, exists = Language.objects.get_or_create(creator=choice(USERS),
                                  language='german')
  except IntegrityError:
      pass
  </code></pre>
  
  <p>But <code>django admin</code> throws <code>IntegrityError</code> when I try to create a language instance through django admin.</p>
  
  <p><strong>My question: How do I implement something like the <code>try/except</code> block above inside my model to check if a language already exists when a user tries to create one?</strong> </p>
  
  <p>I've done a lot of searching and have come across pre_save from <a href="https://docs.djangoproject.com/en/1.11/ref/signals/#pre-save" rel="nofollow noreferrer">signals</a> which I suspect might be helpful. But the django docs have very little by way of examples. </p>
  
  <p>Any help would be appreciated. </p>