|
| 1 | +from collections.abc import Iterable |
1 | 2 | from typing import Any |
2 | 3 |
|
3 | | -from behaviors.behaviors import Timestamped # type: ignore[import-untyped] |
4 | 4 | from django.contrib.contenttypes.models import ContentType |
5 | 5 | from django.db import models |
| 6 | +from django.db.models.base import ModelBase |
| 7 | +from django.utils import timezone |
6 | 8 |
|
7 | 9 |
|
8 | 10 | __all__ = [ |
@@ -42,11 +44,30 @@ def get_label(cls) -> str: |
42 | 44 | return cls._meta.label_lower.split(".")[-1] |
43 | 45 |
|
44 | 46 |
|
45 | | -class TimestampedModel(DefaultModel, Timestamped): |
| 47 | +class TimestampedModel(DefaultModel): |
46 | 48 | """ |
47 | | - Default app model that has `created` and `updated` attributes. |
48 | | - Currently based on https://github.com/audiolion/django-behaviors |
| 49 | + Default app model that has `created` and `modified` attributes. |
49 | 50 | """ |
50 | 51 |
|
| 52 | + created = models.DateTimeField(auto_now_add=True) |
| 53 | + modified = models.DateTimeField(null=True, blank=True) |
| 54 | + |
51 | 55 | class Meta: |
52 | 56 | abstract = True |
| 57 | + |
| 58 | + def save( |
| 59 | + self, |
| 60 | + *, |
| 61 | + force_insert: bool | tuple[ModelBase, ...] = False, |
| 62 | + force_update: bool = False, |
| 63 | + using: str | None = None, |
| 64 | + update_fields: Iterable[str] | None = None, |
| 65 | + ) -> None: |
| 66 | + if self.pk: |
| 67 | + self.modified = timezone.now() |
| 68 | + return super().save( |
| 69 | + force_insert=force_insert, |
| 70 | + force_update=force_update, |
| 71 | + using=using, |
| 72 | + update_fields=update_fields, |
| 73 | + ) |
0 commit comments