Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; '(using': 0.05; 'python': 0.08; 'am,': 0.12; 'stored': 0.13; '(django': 0.16; 'hierarchy': 0.16; 'instances.': 0.16; 'workflow': 0.16; 'workflows': 0.16; 'workflows,': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'subject:question': 0.17; 'subject:skip:d 10': 0.17; 'wrote:': 0.18; 'marc': 0.18; 'this?': 0.19; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; 'input': 0.22; 'assume': 0.22; 'header:In-Reply- To:1': 0.22; 'appear': 0.23; 'unsure': 0.23; 'skip:m 30': 0.24; 'cc:2**0': 0.24; 'correctly.': 0.24; 'code': 0.25; 'classes': 0.26; 'django': 0.26; 'message-id:@mail.gmail.com': 0.28; 'pass': 0.29; 'cc:addr:python.org': 0.29; 'nov': 0.29; 'class': 0.29; 'received:mail-bw0-f46.google.com': 0.30; '\xa0\xa0\xa0': 0.31; 'list': 0.32; 'received:209.85.214': 0.32; 'tue,': 0.32; 'implement': 0.32; 'this.': 0.33; 'ordered': 0.34; 'something': 0.35; '...': 0.36; 'instead.': 0.37; 'skip:" 10': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'somewhat': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'e.g.': 0.39; 'i.e.': 0.39; 'should': 0.39; 'received:209': 0.40; 'more': 0.61; '2011': 0.61; 'expert': 0.62; 'order': 0.62; 'here': 0.65; 'collection': 0.69; 'become': 0.69; '1st': 0.70; 'tracking': 0.70; '29,': 0.73; 'database.': 0.74; 'pro': 0.81; 'composite': 0.84; 'comprised': 0.91; 'constitute': 0.91; 'edwards': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=siU0Y0h7QtAfaDfIBdVQvdx8uEyE+jZAQUVcHTiNTqA=; b=LEcT2dt6q7o38llW6pWX8tpl7aQW7Rggh+H0VGjczMcFBuOu9W4FF5k0i6rZxLMiBb KpedYFNhmms3ox84uRmJ2UoHspXBdMNj9LjHrBvEAiwmV4+oQ4zSzL41MXhvTp1FNKx1 jd3SgOL71mftfPhaRNTN6PIp1GzeNlHUssvyA= MIME-Version: 1.0 In-Reply-To: <4ED4F890.70201@gmail.com> References: <4ED4F890.70201@gmail.com> From: Ian Kelly Date: Tue, 29 Nov 2011 09:38:19 -0700 Subject: Re: Amateur question on class definitions... To: jmarcedwards@gmail.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1322584732 news.xs4all.nl 6850 [2001:888:2000:d::a6]:33208 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16397 On Tue, Nov 29, 2011 at 8:21 AM, J. Marc Edwards w= rote: > So I am defining my Python classes for a Django data model.=A0 I have a t= he > following class hierarchy and would value some expert input on how to bes= t > 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 1= st > AND wf_3 is 2nd. > As I have many workflow instances that accumulate in my persistent databa= se > (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): > =A0=A0=A0 ...a list of "a_workflows", i.e. a composite workflow, should I= use this > syntax? > =A0=A0=A0 set_of_workflows =3D 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 =3D models.ManyToManyField('Workflow', through=3D'CompositeWorkflowOrder') class CompositeWorkflowOrder(models.Model): workflow =3D models.ForeignKey('Workflow') composite_workflow =3D models.ForeignKey('CompositeWorkflow') sequence_num =3D 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