Skip to content

Instantly share code, notes, and snippets.

@marksweb
Last active October 18, 2025 15:29
Show Gist options
  • Select an option

  • Save marksweb/4be5c19f64d45a2be5b52a2461bc9a89 to your computer and use it in GitHub Desktop.

Select an option

Save marksweb/4be5c19f64d45a2be5b52a2461bc9a89 to your computer and use it in GitHub Desktop.
UUID mixin for django models
from uuid import uuid4
from django.db import models
from django.utils.translation import gettext_lazy as _
class UUIDMixin(models.Model):
"""
Mixin for models contain a unique UUID
"""
class Meta:
abstract = True
uuid = models.UUIDField(
verbose_name=_('UUID'),
default=uuid4,
db_index=True,
help_text=_('Unique identifier'),
)
@property
def short_uuid(self):
"""
Return the last 12 hex-digits of the UUID to use as a short identifier
:return: the last 12 digits of the UUID
:rtype: str
"""
return str(self.uuid).split('-')[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment