Skip to content

Instantly share code, notes, and snippets.

@dfrdmn
Created November 3, 2011 15:54
Show Gist options
  • Save dfrdmn/1336851 to your computer and use it in GitHub Desktop.
Save dfrdmn/1336851 to your computer and use it in GitHub Desktop.
Unique token for model
class MyModel(models.Models):
token = models.CharField(max_length=22, unique=True, editable=False)
def save(self, *args, **kwargs):
if not self.token:
import uuid
import base64
self.token = base64.urlsafe_b64encode(uuid.uuid4().bytes).strip('=')
return super(MyModel, self).save(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment