I have a model that has this field on it:
token = models.CharField(max_length=32, default="", unique=True, null=False, db_index=True)
In the save() method I set this field to a 32 character random string using this method:
def save(self, *args, **kwargs):
    if (self.token is None or len(self.token) == 0):
        self.token = random_identifier()
    super(SessionPassthrough, self).save(*args, **kwargs)
def random_identifier(n=32):
    """ Generate a random identifier of length n. 
    From http://stackoverflow.com/questions/2257441/python-random-string-generation-with-upper-case-letters-and-digits"""
    return ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(n))
However I am getting this error whenever I try to create a new instance of the model:
IntegrityError: duplicate key value violates unique constraint "wakelytics_sessionpassthrough_token_key"
To create the instance I call this method:
@staticmethod
def for_session(session):
    sp, c = SessionPassthrough.objects.get_or_create(session=session)
    return sp
Does get_or_create() call the method's save() function before writing to the database? Answer: Yes
I get an IntegrityError whenever I call the method for the first time with a session, and continue getting the error for a few minutes. It then returns correctly. What is causing this?
 
     
    