Check if a unique value exists before creating it

`If you put unique=True in your model, when you try to create an object with same unique = True parameter, error will returned, so make sure that you call them in try-except to handle that.


You can override the save method on that model and check if the value you are trying to save already exists:

class Subject(models.Model):
     name = models.CharField(max_length=64, unique=True)

     def save(self, *args, **kwargs):
         #check if value exists / increment something etc

         #continue with save, if necessary:
         super(Subject, self).save(*args, **kwargs)

You can use get_or_create.

s, created = Subject.objects.get_or_create(name="A new subject")