渲染django中的onetoone关系

时间:2015-09-23 18:59:10

标签: django django-models django-forms formset

我有2个型号:

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 BACnetDevice(models.Model):
    ixBACnetDevice = models.AutoField(primary_key=True)
    ixModbusDevice = models.OneToOneField(ModbusDevice)
    sIP = models.GenericIPAddressField(verbose_name='IP Address')
    iPort = models.IntegerField(verbose_name='Port')
    iSubnet = models.IntegerField(verbose_name='Subnet', blank=True)
    bStatus = models.BooleanField(default=False)

    class Meta:
        db_table = 'TBACnetDevice'

正如您所看到的,每个BACnetDevice通过一对一与每个ModbusDevice相关联。我要做的是显示每个模型中包含混合字段的表格。

我想在ModbusDevice这个模型中显示sIP名称以及iPortiSubnetBACnetDevice(有点像我工作的那样) : enter image description here

我在渲染模型的formset外键字段时遇到了困难

到目前为止,我在我看来已经尝试过了:

BACnetFormSet = inlineformset_factory(ModbusDevice, BACnetDevice, fields=('sIP','iPort','iSubnet',))
bacnet_forms = BACnetFormSet()

return render(
    request,
    'app/create_bacnet.html',
    context_instance = RequestContext(request,
    {
        'title':'Create BACnet Device',
        'tag': 'create_bacnet',
        'bacnet_forms': bacnet_forms
    })
)

0 个答案:

没有答案