Otura
Otura

Reputation: 31

Django mptt: model with a mptt tree instance as a field

I am developing an app with Django where I have to model systems.

A system is defined by a number of standard fields (name, owner, etc...) and then a tree of assets (implemented using mptt). A given asset can be part of several systems. A node in the tree of assets can point only to a given asset.

So, I have a model to represent the assets (simplified):

class Asset(models.Model):
    name = models.CharField(verbose_name = 'name', max_length = 64, unique = True, blank = False, null = False)

I have a class to represent the system trees (simplified):

class System_Tree(MPTTModel):
    parent = TreeForeignKey('self',verbose_name = 'parent', on_delete = models.CASCADE, blank = True, null = True, related_name = 'children')
    asset = models.ForeignKey(Asset, verbose_name = 'asset', on_delete = models.CASCADE, blank = False, null = False)

I have a class to represent the system (simplified):

class System(models.Model):
    name = models.CharField(verbose_name = 'name', max_length = 64, unique = True, blank = False, null = False)

I would like to show in a template the system with its fields and its specific system tree, but I don't know how to relate a specific instance of a system tree (maybe identified by a root node (parent = null)) with the System model.

Should I define a new field type representing system trees?

Should I store only a reference to the parent node id as a big integer field?

Is there any way then to automate the admin interface to show the right forms for a system (the standard fields and then a tree for the system tree)?

Thanks in advance!!

Upvotes: 1

Views: 551

Answers (0)

Related Questions