On Tue, Nov 29, 2011 at 8:21 AM, J. Marc Edwards <jmarcedwa...@gmail.com> wrote: > So I am defining my Python classes for a Django data model. I have a the > following class hierarchy and would value some expert input on how to best > implement this. > > I have many instances of a particular class which I call a "workflow", e.g. > wf_1, wf_2, wf_3, etc. > These class instances of workflows can be combined into "composite > workflows", e.g. {cwf_1 is comprised of wf1 & wf_3}, {cwf_2 is wf_2 and > wf_3}, or {cwf_3 is just wf_2}, etc. > The workflows that constitute the composite workflow is positionally > dependent, i.e. if cwf_1 is comprised of wf_1 and wf_3, then wf_1 comes 1st > AND wf_3 is 2nd. > As I have many workflow instances that accumulate in my persistent database > (Django data model), then the composite workflows become an ordered > collection of workflow instances. > > My first thought here is that the composite workflow class should have a > field that is a simple list of workflows, i.e. list(workflows), but I am > somewhat unsure of how to code this correctly. > > Here is a sample pseudo-code... > > class a_workflow(models.Model): > ... > > class composite_workflow(models.Model): > ...a list of "a_workflows", i.e. a composite workflow, should I use this > syntax? > set_of_workflows = list(a_workflow) > > Is this how a pro would do this?
That would work in general, but since these are Django models I assume that this relationship will need to be stored in the database. You'll need to use a ManyToManyField instead. I would do something like this: class Workflow(models.Model): pass class CompositeWorkflow(models.Model): workflow_set = models.ManyToManyField('Workflow', through='CompositeWorkflowOrder') class CompositeWorkflowOrder(models.Model): workflow = models.ForeignKey('Workflow') composite_workflow = models.ForeignKey('CompositeWorkflow') sequence_num = models.IntegerField() The CompositeWorkflowOrder model is then responsible for tracking which workflows are contained by which composite workflows, and in what order they appear (using sequence_num). See the Django docs on ManyToManyFields for more info. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list