Yes, I overlooked the M2M relation in my rush. Thanks for pointing that out.
I would use your model representations as is, except that the order field in the intermediary model ProcessPhase would be order = models.PositiveIntegerField(unique=True) instead of order = models.PositiveIntegerField() So let me rephrase my original question with the new model definitions: - How do I go about allowing the user to (a) select all the Phases that need to be included in a given Process and (b) specify their respective orders in a single "screen/page"? The way I see it, I would use a ModelMultipleChoiceField to allow the user to select all the phases that they want. But how do I allow the user to specify the order of each selected phase in the same "screen/page"? Regards, CM. On Oct 20, 12:29 pm, "Bogdan I. Bursuc" <bogdanbursu...@gmail.com> wrote: > chefsmart wrote: > > I have a particular scenario that I can't seem to figure out how to > > accomplish in Django. > > > I have the following models: - > > > class Process(models.Model): > > name = models.CharField(max_length=50) > > is_active = models.BooleanField(db_index=True) > > > class Phase(models.Model): > > name = models.CharField(max_length=50) > > is_active = models.BooleanField(db_index=True) > > > class ProcessPhase(models.Model): > > process = models.ForeignKey(Process) > > phase = models.ForeignKey(Phase) > > order = models.PositiveIntegerField(help_text="At what step of > > your process will this phase occur?", unique=True) > > class Meta: > > ordering = ['order'] > > unique_together = (("recruitment", "phase"),) > > > Here an instance of Process can have many instances of Phases, and > > each Phase in a Process needs to be completed in the order specified. > > Different Processes may have their phases in different order, this is > > why the order is specified as part of the ProcessPhase model instead > > of the Phase model. > > > I am having difficulty in implementing the creation of ProcessPhase in > > the UI. Ideally the user would be able to select all the phases that > > need to be included in a given Process and specify their respective > > orders in a single "screen/page". (This is required also because in > > the ProcessPhase model, the order field has unique=True.) > > > Right now I have a ModelMultipleChoiceField for phases so that the > > user may choose the phases that need to be included. I then use a > > second form (a formset really) to allow the user to specify the > > desired order for the selected phases. Then I tried to make it more > > intuitive for the user so that they do not have to go through two > > different forms to achieve the end result, so I explored the > > FormWizard. However, it seems the FormWizard does not support using > > formsets in its steps. > > > So how would you implement this in Django? > > Sorry, but this looks pretty wrong to me. > Dunno if I get this right but if I do you should do this with M2M > relation and additional data to the M2M relation like this: > > class Phase(models.Model): > name = models.CharField(max_length=50) > is_active = models.BooleanField(db_index=True) > > class Process(models.Model): > name = models.CharField(max_length=50) > phases = models.ManyToManyField(Phase, through='ProcessPhase') > is_active = models.BooleanField() > > class ProcessPhase(models.Model): > process = models.ForeignKey(Process) > phase = models.ForeignKey(Phase) > order = models.PositiveIntegerField() > > class Meta: > ordering = ['order'] > # and i think you can skip the unique together, because > # this already imply because of the M2M relation > > Dunno if this is right or needs adjusted, you should read more about > this here: > > http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-m... --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---