Hello everyone, Im quite new in django and I have a problem which i took almost a week and it is still not solved. I have a form that is required to save 3 different models at once but currently I am stuck on getting the 2 model. Appreciate if anyone could solve this.
This are my codes :
(In views.py)
def device_add(request):
if request.method == "POST":
device_frm = DeviceForm(request.POST)
dd_form = DeviceDetailForm(request.POST)
if device_frm.is_valid():
device_frm.save() #Saving form details : Hostname & Ipaddr
deviceid = Device.objects.aggregate(Max('id')) #Getting the ID of saved row for Hostname & Ipaddr
device = Device.objects.all() ##Part A1
if dd_form.is_valid():
deviceD = dd_form.save(commit=False)
deviceD.device = Device.objects.get(id=deviceid)
deviceD.save()
return render(request, 'interface/device-added.html',{'devices':device})
else:
device_frm = DeviceForm()
dd_form = DeviceDetailForm()
di_frm = DeviceInterfaceForm()
return render(request,'interface/device_add.html',{'form':device_frm, 'dd_form': dd_frm})
(models.py)
class Device(models.Model):
hostname = models.CharField(max_length=50)
ipaddr = models.CharField(max_length=15)
date_added = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.hostname
class DeviceDetail(models.Model):
hostname = models.CharField(max_length=50)
mgt_interface = models.CharField(max_length=50)
mgt_ip_addr = models.CharField(max_length=30)
subnetmask = models.CharField(max_length=30)
ssh_id = models.CharField(max_length=50)
ssh_pwd = models.CharField(max_length=50)
enable_secret = models.CharField(max_length=50)
device_model = models.ForeignKey(Device, related_name='Device', on_delete=models.CASCADE)
def __str__(self):
return self.hostname
(forms.py)
class DeviceForm(ModelForm):
class Meta:
model= Device
fields= ['hostname', 'ipaddr'] ##Part A1: date_added is not required as it's value is set by default=timezone.now() in models.py
class DeviceDetailForm(ModelForm):
class Meta:
model= DeviceDetail
fields= ['hostname', 'mgt_interface', 'mgt_ip_addr', 'subnetmask', 'ssh_id', 'ssh_pwd', 'enable_secret', 'device_model']
def clean(self):
cleaned_data = super().clean()
hostname = cleaned_data.get('hostname')
if len(hostname) < 8:
raise forms.ValidationError(f'Hostname needs to be more than 8 character long, {hostname}')
return cleaned_data
(device_add.html)
{% block content %}
<div class="content-page">
<div class="content">
<!-- start page title -->
<div class="row">
<div class="col-12">
<div class="page-title-box">
<div class="page-title-right">
<button type="button" class="btn btn-light mb-2 mr-1">Download Template</button>
<!--
<ol class="breadcrumb m-0">
<li class="breadcrumb-item"><a href="javascript: void(0);">Hyper</a></li>
<li class="breadcrumb-item"><a href="javascript: void(0);">eCommerce</a></li>
<li class="breadcrumb-item active">Devices</li>
</ol>
-->
</div>
<h4 class="page-title">Add Device</h4>
</div>
</div>
</div>
<!-- end page title -->
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<!--<form action="#"> -->
<form action="/device_add/" method="post"> {% csrf_token %}
<div class="row">
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<label for="{{form.hostname.id_for_label}}">Hostname</label>
{{form.hostname}}
<!-- Fieldame in model -->
<!-- <input class="form-control" type="text"
id="{{device_frm.hostname.id_for_label}}"
name="{{device_frm.hostname.name}}" placeholder=""> -->
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label for="{{dd_form.mgt_interface.id_for_label}}">Management Interface</label>
{{dd_form.mgt_interface}}
</div>
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<label for="{{form.ipaddr.id_for_label}}">IP Address</label>
{{form.ipaddr}}
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label for="{{dd_form.mgt_ip_addr.id_for_label}}">Management IP Address<span
class="text-danger">*</span></label>
{{dd_form.mgt_ip_addr}}
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="{{dd_form.subnetmask.id_for_label}}">Subnet Mask Bit<span
class="text-danger">*</span></label>
<select class="custom-select mb-3" name="{{dd_form.subnet_bit}}">
<option selected>Select</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<label for="{{dd_form.ssh_id.id_for_label}}">SSH/Telnet ID<span
class="text-danger">*</span></label>
{{dd_form.ssh_id}}
</div>
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<label for="{{dd_form.ssh_pwd.id_for_label}}">SSH/Telnet Password<span
class="text-danger">*</span></label>
<div class="input-group input-group-merge">
{{dd_form.ssh_pwd}}
<div class="input-group-append" data-password="false">
<div class="input-group-text">
<span class="password-eye"></span>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label for="{{dd_form.enable_secret.id_for_label}}">Enable Secret<span
class="text-danger">*</span></label>
<div class="input-group input-group-merge">
{{dd_form.enable_secret}}
<div class="input-group-append" data-password="false">
<div class="input-group-text">
<span class="password-eye"></span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<label for="{{dd_form.device_label.id_for_label}}">Model<span
class="text-danger">*</span></label>
<select class="custom-select mb-5" name="{{dd_form.device_label.name}}">
<option selected>Select</option>
<option value="Catalyst 9606R">Catalyst 9606R</option>
<option value="C9300L-48T-4X">C9300L-48T-4X</option>
</select>
</div>
</div>
</div>
<div id ="firstrow">
<div class = "rowsdiv">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label for="model">Module ID</label>
<select class="custom-select mb-3">
<option selected>Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="mgt_ip">First Port ID</label>
<input class="form-control" type="text" id="port_id_start" placeholder="e.g. TenGigabitEthernet1/0/1" >
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="mgt_ip">Last Port ID</label>
<input class="form-control" type="text" id="port_id_end" placeholder="e.g. TenGigabitEthernet1/0/48" >
</div>
</div>
<div class="col-md-1">
<div class="form-group">
<div class="text-sm-center">
<br />
<button type="button" class="btn btn-outline-success btn-rounded" onclick = "addrow();"><i class="dripicons-plus"></i></button>
</div>
</div>
</div>
<div class="col-md-1">
<div class="form-group">
<div class="text-sm-center">
<br />
<button type="button" class="btn btn-outline-danger btn-rounded" onclick = "removerow();"><i class="dripicons-minus"></i></button>
</div>
</div>
</div>
</div>
</div>
</div>
<!--more rows-->
<div id ='morerows'>
</div>
<!--more rows-->
<div class="row"><br /></div>
<div class="row">
<div class="col-md-12">
<div class="text-sm-right">
<button class="btn btn-outline-secondary" type="submit">Save</button>
<button class="btn btn-outline-secondary" type="reset">Clear</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div> <!-- end card-body-->
</div> <!-- end card-->
</div> <!-- end col -->
</div>
<!-- end row -->
</div> <!-- End Content -->
{% endblock content %}