Reputation: 47
I have created this model:
class Process(models.Model):
name = models.CharField('Process name', max_length=50, unique=True)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, max_length=50, null=True, verbose_name='Process owner')
I want to create multiple processes and from those build a hierarchy. Here is a hierarchy model:
class Hierarchy(models.Model):
mainprocess = models.ForeignKey(Process, on_delete=models.CASCADE, related_name='mainprocess', verbose_name='Main process name')
subprocess = models.ForeignKey(Process, on_delete=models.CASCADE, related_name='subprocess', verbose_name='Sub process name')
order = models.IntegerField('Process order', default=1)
EDIT: Got it work with code above and now I can build some type of hierarchy, but there are some issues:
I would like to restrict so that subprocess can be under only one main process. If i create a two different hierarchies with same subprocess it gives an unique contrstraint violation on the subprocess id, so I guess it works now.
I would like to restrict so that main process and sub process cannot be same. Now I can create it, but is there a way to do this in the model itself or do I restrict this on the client side when hierarchy is created?
What would be the right way to build this type of model/hierarchy? This might be very basic issue, but I couldn't find any related issues to apply a solution from, so any help would be appreciated.
EDIT: what i want to accomplish with the hierarchy model is to keep track on the process tree like for example:
1 Process (main process)
1.1 process (sub process)
1.1.1 process (sub process)
1.2 process (sub process)
1.2.1 process (sub process)
2 Process (main process)
2.1 process (sub process)
2.2 process (sub process)
n Process (main process)
n.1 process (sub process)
n.1.1 process (sub process)
n.1.1.1 process (sub process)
n.2 process (sub process)
Upvotes: 0
Views: 96