diff options
author | 2020-08-27 00:59:39 +0200 | |
---|---|---|
committer | 2020-08-27 00:59:39 +0200 | |
commit | 05a0575e28abfebb54a7f3996c182ae6ae091ab6 (patch) | |
tree | 61fa0951297cd0745bda4b95a3c40f04e042f4ad /pydis_site/apps/api/models/mixins.py | |
parent | OT: Rename variable `ext` to `other_names` (diff) | |
parent | Merge pull request #374 from Numerlor/reminder-direct-retrieve (diff) |
Merge branch 'master' into off-topic-non-random
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 |