Skip to content

Instantly share code, notes, and snippets.

@mushonnip
Last active December 5, 2023 22:54
Show Gist options
  • Save mushonnip/0423a2288979d48b772f67835c3e5236 to your computer and use it in GitHub Desktop.
Save mushonnip/0423a2288979d48b772f67835c3e5236 to your computer and use it in GitHub Desktop.
Django model to generate unique id/ booking id/ transaction id
import uuid
class Booking(models.Model):
booking_no = models.CharField(primary_key=True, default=uuid.uuid4().hex[:5].upper(), max_length=50, editable=False)
def __str__(self):
return str(self.booking_no)
@123epsilon
Copy link

Django now also has a UUIDField:

models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment