title: Properly Generating Unique Identifier for Django Model
body: <p>I did the following in my <code>model.py</code> to generate a unique identifier for it:</p>
  
  <pre><code> 12 def rand_key(size):
   13     return ''.join([random.choice(string.letters + string.digits) for i in range(size)])
   14 
   15 
   16 class Share(models.Model):
   17 
   20     resource_content_type = models.ForeignKey(ContentType)
   21     resource_object_id = models.PositiveIntegerField()
   22     resource_content_object = generic.GenericForeignKey('resource_content_type', 'resource_object_id')
   23 
   24     identifier = models.CharField(max_length=16,unique=True,default=rand_key(16))
  </code></pre>
  
  <p>The thing is that when a try to save consecutively two records, the error message <code>duplicate key value violates unique constraint "share_share_identifier_key"</code> pop up. I'm sure that this is a common feature. </p>
  
  <p>What I'm doing wrong? </p>
  
  <p>Thanks</p>
  
  <p>Edit: Let me just add the the first record is created without any problem and the identifier field has a propoerly "random" value. Is in the cration of the second record that happens the clash. It seems that the <code>rand_key</code> function is somehow beeing cached. </p>