Reputation: 1169
I'm bulding a model with nested tree classification. A vocabulary Entity
is inherited from an abstract base class TreeVocabulary
. There is also a class SpecialEntity
, which inherits from Entity
. In SpecialEntity
there should be few additional fields.
Entity
and SpecialEntity
should both be trees, for which I use mptt http://django-mptt.github.com/django-mptt/. In Entity
there should be a record, which has children in the SpecialEntity
(those children are root elements in the SpecialEntity
).
This is how i imagine this:
class Vocabulary(models.Model):
name= models.CharField(max_length=300)
order= models.IntegerField(default=100)
class Meta:
abstract= True
class SpecialType(Vocabulary):
class TreeVocabulary(MPTTModel):
parent= TreeForeignKey('self', null=True, blank=True,
related_name='children', limit_choices_to=Q(parent__isnull=True))
class MPTTMeta:
order_insertion_by= ('name',)
class Meta:
abstract= True
class Entity(TreeVocabulary):
class SpecialEntity(Entity):
code= models.CharField(max_length=50)
type= models.ForeignKey(SpecialType)
class Meta:
ordering= ('code',)
Now, the problem is that for some reason SpecialEntity
escapes mptt: sqlall
shows a plain table without parent_id
in it. Although it's present in Entity
, which directly inherits from TreeVocabulary
.
Is it a bug in django-mptt? Or maybe it is a bad design? I'm not asking to design it for me, but rather to point in the right direction
Thanks in advance!
Upvotes: 2
Views: 1065
Reputation: 1169
Ok, answering my own question after a short investigation.
Multi-table inheritance is possible in mptt, but all mptt fields (parent, lft, rght, level, etc.) for any child should be stored in the one (obviously, parent) table.
Taking into consideration the principles of Modified Preorder Tree Traversal, it is reasonable.
For the example in my question in the db there will be created:
specialtype (plain table)
entity - tree structure with the mptt fields, containing data for both models Entity and SpecialEntity
specialentity - plain table with foreign key to entity, containing only fields specific to SpecialEntity
Upvotes: 1