Skip to content

Instantly share code, notes, and snippets.

@orb
Created January 29, 2011 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orb/801935 to your computer and use it in GitHub Desktop.
Save orb/801935 to your computer and use it in GitHub Desktop.
An automatic UUID Field
from django.db import models
from django_extensions.db.fields import UUIDField
class AutoUUIDField(UUIDField):
def contribute_to_class(self, cls, name):
assert not cls._meta.has_auto_field, "A model can't have more than one AutoField."
super(UUIDField, self).contribute_to_class(cls, name)
cls._meta.has_auto_field = True
cls._meta.auto_field = self
class MyModel(models.Model):
id = AutoUUIDField(primary_key=True)
# etc...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment