Hi everyone,
I need your opinions about my models to build good and robust models by django.
I have the following models
# make a generic model
class BasicData(models.Model):
user = models.ForeignKey(
"users.CustomUser",
on_delete=models.CASCADE
)
active = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
is_deleted = models.BooleanField(default=False, verbose_name=_('Deleted'))
deleted_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class CreateObject(BasicData):
name = models.CharField(verbose_name="Name", max_length=100)
type = models.CharField(verbose_name="Type", max_length=100, choices=OBJECT_TYPE)
account_no = models.CharField(
verbose_name="Account No.",
max_length=100,
blank=True, null=True
)
updated_user = models.ForeignKey(
"users.CustomUser",
related_name="user_make_create_object_changes",
on_delete=models.CASCADE
)
class Meta:
verbose_name = _("create object")
verbose_name_plural = _("Create Object")
indexes = [
models.Index(fields=["name"]),
models.Index(fields=["type"]),
models.Index(fields=["account_no"]),
models.Index(fields=["active"]),
models.Index(fields=["created_at"]),
models.Index(fields=["updated_at"]),
models.Index(fields=["is_deleted"]),
models.Index(fields=["deleted_at"]),
]
def __str__(self):
return f"{self.name} - {self.type}"
I tended to use (CreateObject model) to build any model has the same fields(to reduce models coding), but I like to ask first in order to ensure that I have a robust models design.
- Here I have some questions (if I made the index for(active, create_at, updated_at, is_deleted and deleted_at)) in (BasicData) under class Meta:
- Will it apply in all models which inherit from (BasicData) and I don’t need to write it again and again in all models?
The next models have the same fields and I have others.
class UnitNames(BasicData):
name = models.CharField(max_length=100)
updated_user = models.ForeignKey(
"users.CustomUser",
related_name="user_make_unit_names_changes",
on_delete=models.CASCADE,
)
class Meta:
app_label = "configurations"
verbose_name = "unit_names"
verbose_name_plural = "UnitNames"
indexes = [
models.Index(fields=["name"]),
models.Index(fields=["active"]),
models.Index(fields=["created_at"]),
models.Index(fields=["updated_at"]),
models.Index(fields=["is_deleted"]),
models.Index(fields=["deleted_at"]),
]
def __str__(self):
return "{}".format(self.name)
class Branches(BasicData):
name = models.CharField(max_length=100, unique=True)
description = models.TextField(blank=True, null=True)
updated_user = models.ForeignKey(
"users.CustomUser",
related_name="user_make_branches_changes",
on_delete=models.CASCADE,
)
class Meta:
app_label = "configurations"
verbose_name = "branches"
verbose_name_plural = "Branches"
indexes = [
models.Index(fields=["name"]),
models.Index(fields=["active"]),
models.Index(fields=["created_at"]),
models.Index(fields=["updated_at"]),
models.Index(fields=["is_deleted"]),
models.Index(fields=["deleted_at"]),
]
def __str__(self):
return f"{self.name}"
class PosStation(BasicData):
branch = models.ForeignKey(
Branches,
related_name="pos_branch",
verbose_name=_("Branch"),
on_delete=models.CASCADE,
)
name = models.CharField(max_length=100, unique=True, default="Text")
description = models.TextField(blank=True, null=True, default="Text")
updated_user = models.ForeignKey(
"users.CustomUser",
related_name="user_make_pos_station_changes",
on_delete=models.CASCADE,
)
class Meta:
app_label = "configurations"
verbose_name = "pos_station"
verbose_name_plural = "PosStations"
ordering = ["-id"]
indexes = [
models.Index(fields=["name"]),
models.Index(fields=["active"]),
models.Index(fields=["created_at"]),
models.Index(fields=["updated_at"]),
models.Index(fields=["is_deleted"]),
models.Index(fields=["deleted_at"]),
]
def __str__(self):
return f"{self.name}"
class Departments(BasicData):
name = models.CharField(max_length=100)
updated_user = models.ForeignKey(
"users.CustomUser",
related_name="user_make_departments_changes",
on_delete=models.CASCADE,
)
class Meta:
app_label = "configurations"
verbose_name = "departments"
verbose_name_plural = "Departments"
indexes = [
models.Index(fields=["name"]),
models.Index(fields=["active"]),
models.Index(fields=["created_at"]),
models.Index(fields=["updated_at"]),
models.Index(fields=["is_deleted"]),
models.Index(fields=["deleted_at"]),
]
def __str__(self):
return f"{self.name}"
Kindly if you have any suggestions to enhance my models, I will appreciate it
Thanks,