Thanks for the detail explanation. I understand the concept that you are trying to teach me. But the raw SQL statement I am using is creating tables. This is because the task assign to me was to create new tables on the go after each successful creation of a device. Due to the tables being created from raw sql, i am unable to use all the different function like .filter()
and such which i am forced to use raw sql statements. And also apologies about .all
issue. I re-read my code and realize i was using .get(pk=pk)
.
For the new issue, I will explain my html from scratch. So i have 2 pages.
The 1st page looks like this :
Upon selecting the hostname and press Go, the 2nd page appears as:
Within the HTML of this page, those drop down box, text areas are by itself. It is not linked to any django form function.
A explanation on how user are suppose to use this page,
From Port ID
and
To Port ID
contains the interfaces register. Such as
TenGigabitEthernet1/0/1,TenGigabitEthernet1/0/2
and more. So lets say for example, user select
TenGigabitEthernet1/0/1
for
From Port ID
and
TenGigabitEthernet1/0/5
for
To Port ID
Within the port range textarea, it will list out the following:
TenGigabitEthernet1/0/1,TenGigabitEthernet1/0/2,TenGigabitEthernet1/0/3,TenGigabitEthernet1/0/4TenGigabitEthernet1/0/5
. This is done via scripts i coded in the html. Then user are to continue selecting the mode and so on.
Upon saving, here is what happen:
Within the textarea of
Port Parameter 1
and
Port Paramenter 2
, if the user keys in multiple lines, the lines will be replaced with
\n
.
So for example user keys in :
switch access vlan1
switch trunk native vlan 5
It has to be changed on my side to
switch access vlan 1\nswitch trunk native vlan 5
Moving on to the view side,
views.py
def device_port_selected(request, pk):
devices = Device.objects.get(pk=pk)
tablename = 'dev_interface_'+str(pk)
cursor=connection.cursor()
cursor.execute(f"SELECT interface FROM {tablename} WHERE id >=2")
righttable = cursor.fetchall()
cursor.close()
if request.method == "POST":
job = JobForm(request.POST)
if job.is_valid():
hostname = devices.id
new_job = job.save(commit=False)
selection=request.POST.get('portrange','')
mode=request.POST.get('portmode','')
status=request.POST.get('portstatus','')
portpara1 = request.POST.get('portpara1','')
portpara2 = request.POST.get('portpara2','')
if selection == "":
messages.warning(request, "Please select the ports that you want to configure")
return render(request, 'interface/device_port_selected.html',{'devices':devices, 'righttable':righttable, 'job':job} )
combined={"port_range":selection, "port_mode":mode, "port_status":status, "port_param1":portpara1, "port_param2": portpara2}
combinedfinal = {"device":hostname, "configuration":combined}
job.combinedparameters=str(combinedfinal)
new_job.combinedparameters = job.combinedparameters
new_job.save()
return redirect('/device/', {'device':Device.objects.all, 'devices':device})
else:
print(job.errors)
return render(request, 'interface/device_port_selected.html',{'devices':devices, 'righttable':righttable} )
else:
job=JobForm()
return render(request, 'interface/device_port_selected.html',{'devices':devices, 'righttable':righttable, 'job':job} )
As you can see in the views part. I am requesting individual textareas and textinput data to variables. With those variables, I need to further change it to a format I am requested and the format is
{'device': 177, 'configuration': {'port_range': 'TenGigabitEthernet1/0/1,TenGigabitEthernet1/0/2,TenGigabitEthernet1/0/3,TenGigabitEthernet1/0/4,TenGigabitEthernet1/0/5', 'port_mode': 'Access', 'port_status': 'Disabled', 'port_param1': 'Test\\n1\\n2\\n3', 'port_param2': 'Test\\n1\\n2\\n3'}}
The above format is then saved to my table under combinedparameters
.
models.py
class Job(models.Model):
datetime = models.DateTimeField(default=timezone.now)
combinedparameters = models.CharField(max_length = 1000)
So the task I am asked to do now is to do the exact same thing but now using API. To start: I create the serializer first which i learnt.
serializers.py
class JobSerializers(serializers.ModelSerializer):
class Meta:
model = Job
fields = ['combinedparameters']
Moving on to view, in this case, it will be POST instead of GET or DELETE or PUT. So code is:
views.py
@api_view[('POST')]
def createjob (request, pk=None)
if pk !=None:
I have no idea how to continue because in the previous GET API i made, it is using linking to a model and to its fields. So like example for my device model:
class Device(models.Model):
hostname = models.CharField(max_length=50, unique = True)
ipaddr = models.GenericIPAddressField(protocol='ipv4', unique=True, verbose_name='mangement IP') ##Use for mgt_id_addr
date_added = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.hostname
The API knows one field = one item. Such as it will come out for
"device": [
{
"id": 181,
"hostname": "Testing321",
"ipaddr": "1.1.1.2",
"date_added": "2021-10-05T12:00:29.103527+08:00"
}
]
So going by the same logic of one field = one item. In my POST Api, how do i key in the same amount of inputs like my html when there is only 1 field of combinedparameters
. I dont know if I am making sense of this but basically from what i think, within the API if i manage to load it, it will only have one textbox labeled as combineparameters
. And that is a problem because I cannot make the user type the long format. I assume the API can make it appear multiple textboxes for user to key is like the html which is guided. Then at the back, it is taking the inputs and converting to the format desired and save it.