我有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
名称以及iPort
,iSubnet
和BACnetDevice
(有点像我工作的那样) :
我在渲染模型的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
})
)