title: How do I catch IntegrityError in Django when using UniqueConstraint across multiple fields?
body: <p>I have a model with user as 1 field (Foreign Key) and one other field skill_group. I need to make sure the user does not add duplicate skill groups so I added a UniqueConstraint. This is working as the system errors out with IntegrityError at /skillgroup/create/
  duplicate key value violates unique constraint "unique_skillgroup" - How do I catch this exception and notify user if duplicate; otherwise save it?</p>
  
  <p>New to Django/Python/Postgres and I thought I could handle it by overriding the save() function, but there is no access to user which is part of the check and I have read this should not be handled here. Is there a try/save catch/message I should be employing? I have tried a few things with no luck. I have seen similar questions on here, but they have not helped. Any help is appreciated.</p>
  
  <pre><code>models.py
  class SkillGroup(models.Model):
      user = models.ForeignKey(User, on_delete=models.CASCADE)
      skill_group = models.CharField(max_length=35)
      sequence = models.IntegerField(default=999)
  
      class Meta:
          constraints = [
              models.UniqueConstraint(fields=['user', 'skill_group'], name='unique_skillgroup'),
          ]
  
      def __str__(self):
          return self.skill_group
  
      def get_absolute_url(self):
          return reverse('skillgroup-list')
  </code></pre>
  
  <pre><code>views.py
  class SkillGroupCreateView(LoginRequiredMixin, CreateView):
      model = SkillGroup
      fields = ['skill_group']
  
      def form_valid(self, form):
          form.instance.user = self.request.user
          form.instance.sequence = SkillGroup.objects.filter(user=self.request.user).order_by('sequence').last().sequence + 1
          return super().form_valid(form)
  </code></pre>
  
  <pre><code>skillgroup_form.html
  {% extends "recruiter/baseskills.html" %}
  {% load crispy_forms_tags %}
  
  {% block content%}
    &lt;div class="content-section"&gt;
        &lt;form method="post"&gt;
          {% csrf_token %}
          &lt;fieldset class="form-group"&gt;
            &lt;legend class="border-bottom mb-4"&gt;Skill Group&lt;/legend&gt;
            {{ form|crispy }}
          &lt;/fieldset&gt;
          &lt;div class="form-group"&gt;
            &lt;button class="btn btn-outline-info" type="submit"&gt;Add Skill Group&lt;/button&gt;
          &lt;/div&gt;
        &lt;/form&gt;
    &lt;/div&gt;
  {% endblock content%}
  </code></pre>
  
  <p>I want to either catch the exception and save the record if not a duplicate or put message on screen saying "Skill Group already exists" and leave user on create page. Also, I could remove the UniqueConstraint and handle with code if that is the best solution.</p>