title: Parent formset with childrens inline formsets
body: <p>I'm trying to come up with the best approach for this problem. I have 2 models with 2 forms and 1 formset:</p>
  
  <pre><code>#models
  class ModbusDevice(models.Model):
      ixModbusDevice = models.AutoField(primary_key=True)
      sModbusName = models.CharField(verbose_name='Device Name',max_length=100)
      iPort = models.IntegerField(verbose_name='Port')
      iSlave = models.IntegerField(verbose_name='Slave ID')
      sIP = models.GenericIPAddressField(verbose_name='IP Address')
  
      class Meta:
          db_table = 'TModbusDevice'
  
  
  class Register(models.Model):
      ixRegister = models.AutoField(primary_key=True)
      sRegisterName = models.CharField(max_length=100)
      iStartingAddr = models.IntegerField()
      bRange = models.BooleanField(default=False)
      ixIOType = models.ForeignKey(IOType)
      ixModbusDevice = models.ForeignKey(ModbusDevice)
      iOffset = models.IntegerField(blank=True, null=True)
  
      class Meta:
          db_table = 'TRegister'
  
  #forms
  class InitialRegisterFormset(BaseModelFormSet):
      def __init__(self, *args, **kwargs):
          super(InitialRegisterFormset, self).__init__(*args, **kwargs)
          self.queryset = Register.objects.all()
  
  class ModbusRegistersForm(ModelForm):
      ixIOType = ModelChoiceField(queryset=IOType.objects.all()),
      bRange = BooleanField(required=False)
      bRange.widget.attrs['data-form'] = 0
  
      class Meta:
          model = Register
          fields = ['sRegisterName','iStartingAddr','bRange','ixIOType','iOffset']
          widgets = {
                  'sRegisterName': TextInput(attrs={'placeholder': 'Register Name','class': 'form-control', 'data-form': '0'}),
                  'iStartingAddr': TextInput(attrs={'placeholder': 'Starting address','class': 'form-control','data-form': '0'}),
                  'iOffset': TextInput(attrs={'placeholder': 'Address offset','class': 'form-control','data-form': '0'}),
              }
  class CreateModbusForm(ModelForm):
      class Meta:
          model = ModbusDevice
          fields = ['sModbusName','iPort', 'iSlave', 'sIP']
          widgets = {
                  'sModbusName': TextInput(attrs={'placeholder': 'Device Name','class': 'form-control','data-form': '0',}),
                  'iPort': TextInput(attrs={'placeholder': 'Port','class': 'form-control','data-form': '0',}),
                  'iSlave': TextInput(attrs={'placeholder': 'Slave id','class': 'form-control','data-form': '0',}),
                  'sIP': TextInput(attrs={'placeholder': 'Modbus IP address','class': 'form-control','data-form': '0',})
              }
  </code></pre>
  
  <p>What I'm trying to do is create a formset of ModbusDevices along with their belonging registers(inline formset).</p>
  
  <p>So far this is what I've managed to get:
  <a href="https://i.stack.imgur.com/bOxJ4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bOxJ4.png" alt="enter image description here"></a></p>
  
  <p>I'm returning all existing modbus devices(in this example I only have 1 in my db) and 1 extra if the user wants to add a new device. In my registers is where I have a problem. Right now I'm returning all registers in my table and displaying them instead of displaying only the related registers. I'm not sure how I can go about iterating my device formset and accessing it's children belonging to the device. </p>
  
  <p>Here is my view:</p>
  
  <pre><code>ModbusFormset = modelformset_factory(ModbusDevice, form=CreateModbusForm)
  
  register_qset = Register.objects.all()
  InlineRegisterFormset = None
  if not register_qset:
      InlineRegisterFormset = inlineformset_factory(ModbusDevice, Register, form=ModbusRegistersForm, extra=1)
  else:
      InlineRegisterFormset = inlineformset_factory(ModbusDevice, Register, form=ModbusRegistersForm, formset=InitialRegisterFormset,  extra=len(register_qset))
  
  modbus_qset = ModbusDevice.objects.all()
  devices = ModbusFormset(queryset=modbus_qset, prefix="modbus")
  
  registers = InlineRegisterFormset(prefix="register")
  </code></pre>