Basically my question stems from the need to create multiple models that branch off a main base class. Each of these models have a type, and I wish to keep this type on the model in the database, but I want to room for error so I thought of hardcoding it into the model.
This is pretty much a question of "Is this something that django supports?". I haven't fully tested this, I don't know / can't say that if I did this right now everything would be fine or not ... but I can't seem to find the relevant documentation for it and would like to learn the best practices.
Here is an example of what I mean:
from django.db import models
from enum import Enum
class PersonType(Enum):
STUDENT = "Student"
TEACHER = "Teacher"
class BaseClass(models.Model):
"""Represents a base model"""
name = models.TextField()
age = models.IntegerField()
class Student(BaseClass):
"""Represents a student"""
type = PersonType.STUDENT.value
class Teacher(BaseClass):
"""Represents a teacher"""
type = PersonType.TEACHER.value
Would something like this be accepted by django? The only other solution I can think of is to make type a TextField with the default property set, and never expose it via the serializer. There is no reason I can't do that, just checking that this could work as well since it's, aesthetically, more pleasing.