diff options
author | 2020-08-27 05:47:47 +0000 | |
---|---|---|
committer | 2020-08-27 05:47:47 +0000 | |
commit | feafa0950d9132350e36f58fbae57121363d3277 (patch) | |
tree | ff5db4b51e4246bd2c19cabdccb0fac7e55eacdf /pydis_site/apps/api/models/mixins.py | |
parent | Fix FilterList model mixins import path (diff) |
Still move mixins back to its own file
Diffstat (limited to 'pydis_site/apps/api/models/mixins.py')
-rw-r--r-- | pydis_site/apps/api/models/mixins.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/pydis_site/apps/api/models/mixins.py b/pydis_site/apps/api/models/mixins.py new file mode 100644 index 00000000..5d75b78b --- /dev/null +++ b/pydis_site/apps/api/models/mixins.py @@ -0,0 +1,31 @@ +from operator import itemgetter + +from django.db import models + + +class ModelReprMixin: + """Mixin providing a `__repr__()` to display model class name and initialisation parameters.""" + + def __repr__(self): + """Returns the current model class name and initialisation parameters.""" + attributes = ' '.join( + f'{attribute}={value!r}' + for attribute, value in sorted( + self.__dict__.items(), + key=itemgetter(0) + ) + if not attribute.startswith('_') + ) + return f'<{self.__class__.__name__}({attributes})>' + + +class ModelTimestampMixin(models.Model): + """Mixin providing created_at and updated_at fields.""" + + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta: + """Metaconfig for the mixin.""" + + abstract = True |