Hi Paul, So long as the common model is not abstract, it should work. Also, the common model must be the one that contains the parent field, and it should link to "Node" and not "self".
Using my first example, you should be able to traverse the tree using Node.parent and Node.children. Traversing the tree using Job.parent, Job.children, Shot.parent or Shot.children will not work. I've implemented the first example in one of my own apps, and it does work! :P David. paul wrote: > hi david, > > many thanks for your help there. i initially tried the inheritance > method - buut found that my tree traversal broke when trying to > traverse a tree made of different types (ie, Node & Jobs). maybe i > did something wrong? ie, should i be able to make a tree from those > two types if derived from a common model. or is each derived model > incompatible with each other? > > cheers, > > paul > > > On Oct 7, 11:16 am, David Hall <[EMAIL PROTECTED]> wrote: >> Hi Paul, >> >> You've got a couple of options for this. The first uses inheritance: >> >> class Node(models.Model): >> parent = models.ForeignKey("Node", blank=True, null=True, >> related_name="children") >> >> class Job(Node): >> pass >> >> class Shot(Node): >> pass >> >> The second uses generic foreign keys: >> >> class Job(models.Model): >> parent_type = models.ForeignKey("contenttypes.ContentType") >> parent_id = models.PositiveIntegerField(db_index=True) >> parent = GenerticForeignKey("parent_type", "parent_id") >> >> class Shot(models.Model): >> parent_type = models.ForeignKey("contenttypes.ContentType") >> parent_id = models.PositiveIntegerField(db_index=True) >> parent = GenerticForeignKey("parent_type", "parent_id") >> >> Personally, I'd go with the first, as it will integrate better with the >> Djano admin application. To traverse the tree, use the Node model. >> Once you have got to your required location in the tree, use node.job or >> node.shot to get the rest of the information. >> >> David. >> >> >> >> paul wrote: >>> hi, >>> I'm designing my first django app and would appreciate any advice how >>> best to organise my models. >>> I have a hierarchal model called Node. this allows me to create a >>> hierarchy of nodes - and perform hierarchal operatoins. So far so >>> good. each node can be of two 'types': >>> 1/ job >>> 2/ shot >>> I did originally try and make shot & job inherit the Node model. >>> However, they couldn't 'link' together (ie, using self as a >>> ForeignKey) - as they're of different types. So i've made each of >>> these two node types into seperate models. each Node should only be >>> linked to one type at a time. From the manual/book - i belive that >>> GenericForeignKey is the way to go. ie, insert a GenericForeignKey >>> field into the Node. Does this sound like a good approach? >>> Many Thanks in Advance, >>> Paul >> -- >> David Hall >> Technical Lead >> Etianen.com >> Tel: 07896 106290 >> >> Email [EMAIL PROTECTED] >> Web www.etianen.com >> ------------------------------------------------------------------- >> Ask for help at [EMAIL PROTECTED] >> Etianen.com is a small, professional web development agency that >> specialises in fast-paced, creative development. >> ----------------- enlightened website development ----------------- > > -- David Hall Technical Lead Etianen.com Tel: 07896 106290 Email [EMAIL PROTECTED] Web www.etianen.com ------------------------------------------------------------------- Ask for help at [EMAIL PROTECTED] Etianen.com is a small, professional web development agency that specialises in fast-paced, creative development. ----------------- enlightened website development ----------------- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---