父级formset与儿童内联formset

时间:2015-10-02 14:17:40

标签: html django django-forms formset inline-formset

我正在努力想出这个问题的最佳方法。我有2个模型,2个表单和1个formset:

#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',})
            }

我要做的是创建一个ModbusDevices的模板集及其所属的寄存器(内联formset)。

到目前为止,这是我设法得到的: enter image description here

我将返回所有现有的modbus设备(在此示例中,我的数据库中只有1个),如果用户想要添加新设备,则返回1个额外设备。在我的寄存器中我遇到了问题。现在我将返回表格中的所有寄存器并显示它们,而不是只显示相关的寄存器。我不知道如何迭代我的设备formset并访问属于该设备的子设备。

以下是我的观点:

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")

0 个答案:

没有答案