Django: Dynamic inline forms with filter upon user selection
<p>I have created these models:</p>

<pre><code>class Service(models.Model):
    name = models.CharField(blank=False, max_length=200)code here

class Monitor(models.Model):
    name = models.CharField(blank=False, max_length=100)
    services = models.ManyToManyField(Service, related_name='monitors')

class Student(models.Model):
    name = models.CharField(blank=False, max_length=100)

class ServiceMonitors(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    service = models.ForeignKey(Service, on_delete=models.CASCADE)
    monitors = models.ManyToManyField(Monitor)
</code></pre>

<p>These models represent a company that offer many services to students. Monitors that work for this company are assigned to students who need the service they offer. A monitor can offer many services and have many students for every one of them. Also, a student can have many services and monitors. </p>

<p>Let's suppose the we have 3 services, s1, s2 and s3. When we create a new Monitor we assign him/her some of these services, let's say s1 and s3. Then, when we create a new Student, we should be able to choose which services and monitors the student will have.</p>

<p>My problem here is that I need a form to create the student (name, phone, etc...) that allows me to:</p>

<ol>
<li>Select a service from a dropdown control   </li>
<li>Once a service is selected, the form must show the Monitors that offer that service, so I can choose one or more of them (checkboxes).   </li>
<li>A + button to create/show a new form to repeat the process, so I
can choose a new service and the related Monitors.</li>
</ol>

<p>I'm very new to Django. I've been able to create an inline form in the user creation form, but i'm stuck here. I have now a dropdown control to select a service, and a ModelMultipleChoiceField with a CheckboxSelectMultiple widget which shows all Monitors. From here I need to know how to filter these Monitors when the user selects a service, and how to add new forms to select new services/monitors.</p>

<p>This likely can not be managed within the Django forms framework which is very limited in functionality. It allows you to use a filtered queryset for selections in a ModelChoiceDropDown (I think that's the name) but filtering after a selection isn't possible this way. Django's forms are generated and sent as HTML which can not be made to filter without many modifications.</p>

<p>The way you would likely do this is to set up a REST interface (See Django Rest Framework or Django-Tastypie) and link your dropdowns to that using a front end framework of your choosing (I prefer Backbone.js, but there are many options). The idea being that once you select a service the subsequent dropdowns will add a filter to their requests to only show the Monitors that offer that service. </p>

<p>If you wish to keep using Django Forms you can, but you'll need to extend the default fields to get this sort of functionality.</p>

<p>This is a fairly big rabbit hole and I don't have any resources off the cuff to point you toward. But reading up about REST interfaces is a good start.</p>

python django forms inline-formset