So I am starting a school project, and instead/alongside of using django's id, I want to use a Snowflake id (the kind of ID discord/twitter uses). I tried subclassing BigAutoField , here's my code:
from snowflake import SnowflakeGenerator, Snowflake # i can link this if needed
class SnowflakeField(models.BigAutoField):
__generator: ClassVar[SnowflakeGenerator] = SnowflakeGenerator(1)
def __init__(self, *args, **kwargs):
kwargs['blank'] = False
kwargs['editable'] = False
kwargs['unique'] = True
kwargs['primary_key'] = True
kwargs['default'] = SnowflakeField.__generator.__next__()
super().__init__(*args, **kwargs)
def db_type(self, connection):
return 'bigint'
def pre_save(self, model_instance, add):
if add:
sf = SnowflakeGenerator(1)
else:
return super().pre_save(model_instance, add)
I am making a base model all my other models will mixin/inherit from:
class BaseMixin(models.Model):
"""Mixin/Abstract class for all models to inherit from to add common fields (id, datelogged, lastupdated)"""
id = SnowflakeField()
datelogged = models.DateTimeField(auto_now_add=True)
lastupdated = models.DateTimeField(auto_now=True)
#system = models.BooleanField(default=False)
# we can use is_superuser for this
class Meta:
abstract = True
I know that django gives your row an ID already, is it possible to overwrite it (like i did it above)?
Snowflake IDs, or snowflakes, are a form of unique identifier used in distributed computing. The format was created by Twitter and is used for the IDs of tweets. It is popularly believed that every snowflake has a unique structure, so they took the name "snowflake ID". The format has been adopted by other companies, including Discord and Instag...