Wednesday 15 April 2015

Modeling hierarchical data in django with polymorphic node types -


I have two basic models, a command and flow stream can have a series of commands or other nested flows, so anyone The given flow can be a list of children who are either stage or flow type (this is similar to files and directories that you can model in the file system.)

Generic relationships, and MTPT (which is the general content type AFAIK I do not allow it) but success has not been found here are my original models:

  class phase (models.Model): parent = model. ('Step', tap = true) name = model.carfield (max_length = 100) start_time = Models.DateTimeField (null = true) end_time = models.DateTimeField (null = true) state = models.CharField (max_length = 1, default = 'U') Category flow: Type = Model.Carfield (max_length = 1) get def (child): # todo: Returns the steps given here Step.objects.filter (parent_id = self.parent_id) Def run (self A.) the need to be sorted by their order for the child itself. Children (): Print ("Debuts: Run Mad Processing A {0}". Format (child______________________) if this is a flow, then run it #and if it is the command, then it will command Reduce (phase): exec_string = model Textfield ()   

I want to be able to create flows in my app, interrogating children, after which each kind of child should be processed separately (to execute commands For the flow is reprocessed.)

I would appreciate any correction of your co on top of D. which would make it possible or even comments that I The problem flooded for Django Tests have been incorrectly.

Edit: I should add that I am using Python 3.3 and Django dev 1.6)

I finally got an answer through some big help on IRC and wanted to share it in case someone had the same problem

  def getChildren ( Self): Get a list of all the attributes related to # hair models. If the issubclass (rel.field.model, step) and isinstance (rel.field, models.OneToOneField)) objs = self [Ob.children.all in obj] Select_related (* child_attrs.keys ()): # Try to find any child ... child_attrs.values ​​() for the child: sobj = obj .__ dict __. ) If there is no one then: objos (sobj) back objs   

If someone has a cleaner solution, I would love to see it, especially since it seems like a lot of work It seems that the structure should be handled more directly.

No comments:

Post a Comment