Add created_at and updated_at to all models
This commit is contained in:
parent
951c5cfbfb
commit
512b4e0ed9
4 changed files with 113 additions and 5 deletions
|
@ -0,0 +1,89 @@
|
||||||
|
# Generated by Django 5.2b1 on 2025-03-17 06:19
|
||||||
|
|
||||||
|
import django.utils.timezone
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("core", "0001_initial"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="billingentity",
|
||||||
|
name="created_at",
|
||||||
|
field=models.DateTimeField(
|
||||||
|
auto_now_add=True,
|
||||||
|
default=django.utils.timezone.now,
|
||||||
|
verbose_name="Created",
|
||||||
|
),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="billingentity",
|
||||||
|
name="updated_at",
|
||||||
|
field=models.DateTimeField(auto_now=True, verbose_name="Last updated"),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="organization",
|
||||||
|
name="created_at",
|
||||||
|
field=models.DateTimeField(
|
||||||
|
auto_now_add=True,
|
||||||
|
default=django.utils.timezone.now,
|
||||||
|
verbose_name="Created",
|
||||||
|
),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="organization",
|
||||||
|
name="updated_at",
|
||||||
|
field=models.DateTimeField(auto_now=True, verbose_name="Last updated"),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="organizationmembership",
|
||||||
|
name="created_at",
|
||||||
|
field=models.DateTimeField(
|
||||||
|
auto_now_add=True,
|
||||||
|
default=django.utils.timezone.now,
|
||||||
|
verbose_name="Created",
|
||||||
|
),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="organizationmembership",
|
||||||
|
name="updated_at",
|
||||||
|
field=models.DateTimeField(auto_now=True, verbose_name="Last updated"),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="organizationorigin",
|
||||||
|
name="created_at",
|
||||||
|
field=models.DateTimeField(
|
||||||
|
auto_now_add=True,
|
||||||
|
default=django.utils.timezone.now,
|
||||||
|
verbose_name="Created",
|
||||||
|
),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="organizationorigin",
|
||||||
|
name="updated_at",
|
||||||
|
field=models.DateTimeField(auto_now=True, verbose_name="Last updated"),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="user",
|
||||||
|
name="created_at",
|
||||||
|
field=models.DateTimeField(
|
||||||
|
auto_now_add=True,
|
||||||
|
default=django.utils.timezone.now,
|
||||||
|
verbose_name="Created",
|
||||||
|
),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="user",
|
||||||
|
name="updated_at",
|
||||||
|
field=models.DateTimeField(auto_now=True, verbose_name="Last updated"),
|
||||||
|
),
|
||||||
|
]
|
15
src/servala/core/models/mixins.py
Normal file
15
src/servala/core/models/mixins.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
from django.db import models
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
|
class ServalaModelMixin(models.Model):
|
||||||
|
created_at = models.DateTimeField(
|
||||||
|
auto_now_add=True, editable=False, verbose_name=_("Created")
|
||||||
|
)
|
||||||
|
updated_at = models.DateTimeField(
|
||||||
|
auto_now=True, editable=False, verbose_name=_("Last updated")
|
||||||
|
)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
ordering = ("-created_at",)
|
|
@ -1,8 +1,10 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
from .mixins import ServalaModelMixin
|
||||||
|
|
||||||
class Organization(models.Model):
|
|
||||||
|
class Organization(ServalaModelMixin, models.Model):
|
||||||
name = models.CharField(max_length=100, verbose_name=_("Name"))
|
name = models.CharField(max_length=100, verbose_name=_("Name"))
|
||||||
|
|
||||||
billing_entity = models.ForeignKey(
|
billing_entity = models.ForeignKey(
|
||||||
|
@ -33,7 +35,7 @@ class Organization(models.Model):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class BillingEntity(models.Model):
|
class BillingEntity(ServalaModelMixin, models.Model):
|
||||||
"""
|
"""
|
||||||
Every organization has a billing entity, though billing entities
|
Every organization has a billing entity, though billing entities
|
||||||
may be shared across different organizations.
|
may be shared across different organizations.
|
||||||
|
@ -53,7 +55,7 @@ class BillingEntity(models.Model):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class OrganizationOrigin(models.Model):
|
class OrganizationOrigin(ServalaModelMixin, models.Model):
|
||||||
"""
|
"""
|
||||||
Every organization has an origin, though origins may be
|
Every organization has an origin, though origins may be
|
||||||
shared across different organizations. The default origin
|
shared across different organizations. The default origin
|
||||||
|
@ -77,7 +79,7 @@ class OrganizationRole(models.TextChoices):
|
||||||
OWNER = "owner", _("Owner")
|
OWNER = "owner", _("Owner")
|
||||||
|
|
||||||
|
|
||||||
class OrganizationMembership(models.Model):
|
class OrganizationMembership(ServalaModelMixin, models.Model):
|
||||||
"""Through-model for the many-to-many relationship between organizations and users."""
|
"""Through-model for the many-to-many relationship between organizations and users."""
|
||||||
|
|
||||||
user = models.ForeignKey(
|
user = models.ForeignKey(
|
||||||
|
|
|
@ -2,6 +2,8 @@ from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
from .mixins import ServalaModelMixin
|
||||||
|
|
||||||
|
|
||||||
class UserManager(BaseUserManager):
|
class UserManager(BaseUserManager):
|
||||||
"""
|
"""
|
||||||
|
@ -30,7 +32,7 @@ class UserManager(BaseUserManager):
|
||||||
return self.create_user(email, password, **extra_fields)
|
return self.create_user(email, password, **extra_fields)
|
||||||
|
|
||||||
|
|
||||||
class User(AbstractBaseUser):
|
class User(ServalaModelMixin, AbstractBaseUser):
|
||||||
"""The Django model provides a password and last_login field."""
|
"""The Django model provides a password and last_login field."""
|
||||||
|
|
||||||
objects = UserManager()
|
objects = UserManager()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue