I am currently kidding around on how to model something using Django. The basic situation is that I have an object that should serve as a chassis
and provide some sockets
. Then there are loads of different modules
that are placed on the sockets
of a chassis
. I would like to model these different modules
as distinct classes in Django, but use a common class on the database layer, possibly involving some generic fields.
So the database model might look something like this:
class Module(models.model):
name = models.CharField(max_length=128)
# Or is there a better way to annotate a type?
type = models.CharField(max_length=128)
string1 = models.CharField(max_length=128)
string2 = models.CharField(max_length=128)
...
int1 = models.IntegerField()
# Some kind of engine class that derives from Module
# but does nothing else then "mapping" the generic
# fields to something sensible
class Socket(models.Model):
is_on = models.ForeignKey(Chassis)
name = models.CharField(max_length=128)
type = models.CharField(max_length=128)
class Connection(models.Model):
chassis = models.ForeignKey(Chassis)
module = models.ForeignKey(Module)
via = models.ForeignKey(Socket)
class Chassis(models.Model):
name = models.CharField(max_length=128)
modules= models.ManyToManyField(Module, through='Connection')
class Group(models.Model):
name = models.CharField(max_length=128)
Of course I wouldn't want to spoil my logic with this denormalization. Thats why I hinted for some kind of engine
class that should use the Module table, but provide "logical" getters and setters, effectively mapping data like "Horsepower" to "int1".
So my questions are basicly:
- Is what I am doing reasonable with Django? Or is there a better (possibly built in) way to deal with this?
- Would it be possible to construct the correct type, the one providing the wrapper methods for the denormalized model, depending on the
Module.type
field automatically?