Last active
October 18, 2025 15:29
-
-
Save marksweb/4be5c19f64d45a2be5b52a2461bc9a89 to your computer and use it in GitHub Desktop.
UUID mixin for django models
This file contains hidden or 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
| 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