Here is the view:
if request.method == "POST":
device_frm = DeviceForm(request.POST)
dd_form = DeviceDetailForm(request.POST)
di_formset = inlineformset_factory(Device, DeviceInterface, fields=('moduletype', 'firstportid', 'lastportid'),widgets={ 'firstportid':TextInput(attrs={'placeholder': 'e.g. TenGigabitEthernet1/0/1'}), 'lastportid':TextInput(attrs={'placeholder':'eg. TenGigabitEthernet1/0/48'})},extra=10)
di_form=di_formset(request.POST)
if device_frm.is_valid():
new_device = device_frm.save()
if dd_form.is_valid():
deviceD = dd_form.save(commit=False)
deviceD.DD2DKEY = new_device
deviceD.save()
print(deviceD.mgt_interface)
if di_form.is_valid():
deviceI=di_form.save(commit=False)
firstportid0 = request.POST.get('deviceinterface_set-0-firstportid').split('/')[2]
lastportid0 = request.POST.get('deviceinterface_set-0-lastportid').split('/')[2]
list_range=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48]
formatted_list = []
if firstportid0<=lastportid0:
for i in list_range:
#while firstportid0<=lastportid0:
formatted_list.append("TenGigabitEternet1/0/" + str(i))
#firstportid0+=1
print(formatted_list)
cursor = connection.cursor()
string = 'dev_interface_' + str(new_device.id)
# Execute your raw sql
query = f"CREATE TABLE {string}(id integer NOT NULL PRIMARY KEY AUTOINCREMENT, mgt_interface varchar(50) NOT NULL, mgt_ip varchar(15) NOT NULL, interface varchar(50), description varchar(250), speed varchar(250), duplex varchar(250), status varchar(250), mode int(2), vlan varchar(50), voice_vlan varchar(50), trunk_protocol varchar(200), trunk_vlan varchar(50), mtu varchar(50), route_ip varchar(50) );"
cursor.execute(query)
string1=deviceD.mgt_interface
string2= new_device.ipaddr
string3 = 'dev_route_list_' + str(new_device.id)
string4 = 'dev_neighbour_list_' + str(new_device.id)
#queryagain = f"INSERT INTO {string}(mgt_interface, mgt_ip) VALUES ({string1}, {string2})" #Not working
#cursor.execute(queryagain)
queryagain = f"CREATE TABLE {string3}(id integer NOT NULL PRIMARY KEY AUTOINCREMENT, mgt_ip varchar(50) NOT NULL, subnetmask varchar(15) NOT NULL, device_interface_id integer NOT NULL REFERENCES {string} DEFERRABLE INITIALLY DEFERRED );"
queryagainagain = f"CREATE TABLE {string4}(id integer NOT NULL PRIMARY KEY AUTOINCREMENT, mgt_ip varchar(50) NOT NULL, device_interface_id integer NOT NULL REFERENCES {string} DEFERRABLE INITIALLY DEFERRED );"
cursor.execute(queryagain)
cursor.execute(queryagainagain)
cursor.close()
for instances in deviceI:
instances.I2DKEY=new_device
instances.save()
return render(request, 'interface/device_added.html',{'devices':Device.objects.all()})
return render(request,'interface/device_add.html',{'form':device_frm, 'dd_form': dd_form, 'di_form':di_form})
return render(request,'interface/device_add.html',{'form':device_frm, 'dd_form': dd_form, 'di_form':di_form})
return render(request,'interface/device_add.html',{'form':device_frm, 'dd_form': dd_form, 'di_form':di_form})
else:
device_frm = DeviceForm()
dd_form = DeviceDetailForm()
di_formset = inlineformset_factory(Device, DeviceInterface, fields=('moduletype', 'firstportid', 'lastportid'), widgets={ 'firstportid':TextInput(attrs={'placeholder': 'e.g. TenGigabitEthernet1/0/1'}), 'lastportid':TextInput(attrs={'placeholder':'eg. TenGigabitEthernet1/0/48'})},extra=10)
di_form=di_formset(queryset = DeviceInterface.objects.none())
return render(request,'interface/device_add.html',{'form':device_frm, 'dd_form': dd_form, 'di_form':di_form})```
Ignore the raw SQL. For now I have comment out the while statement and the firstportid0+=1. So now it is just looping the whole list_range. If I uncomment it, there will be memory error