Created
November 3, 2011 15:54
-
-
Save dfrdmn/1336851 to your computer and use it in GitHub Desktop.
Unique token for model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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