I have a bunch of classes that I'm now trying to incorporate into django.
For example, I have a Base class that all my other classes derive from:
class Base:
def __init__(self, label: str = 'Base'):
self.label = label
An example of a sublcass would be a Person class:
from typing import Any, Dict
class Person(Base):
def __init__(self, name: str, attributes_to_options: Dict[str, Any], **kwargs):
super().__init__(**kwargs)
self.name = name
self.attributes_to_options = attributes_to_options
I would use this as:
alex = Person(name='Alex', attributes_to_options={'age': 10, 'is_happy': True}, label='Person:Alex')
My question is, how do I incorporate such a class into django? Is it as simple as inheritting from models.Model? e.g.
from django.db import models
class Person(Base, models.Model):
def __init__(self, name: str, attributes_to_options: Dict[str, Any], **kwargs):
super().__init__(**kwargs)
self.name = name
self.attributes_to_options = attributes_to_options
But then how do I specify the models.CharField for the two attributes name and attributes_to_options?
Thanks for any help here.
name = models.CharField(...)etc in the Person class scope? The important thing is that model fields must be declared in a class that inherits frommodels.Model.