Dear All,
I have problem with display record from django . multi devices have multi services and others many-to-many model
models.py
class Service(models.Model):
service_name = models.CharField(max_length=25)
service_port = models.IntegerField(max_length=6)
class Devices(models.Model):
hostname = models.CharField(max_length=20)
address = models.IPAddressField()
type = models.CharField(max_length=10)
os = models.CharField(max_length=20)
servicename = models.ManyToManyField(Service)
view.py
def device(request):
device_list = Devices.objects.all()
#get hostname
c = Context()
#c['info'] = device_list
for i in device_list:
s2 = i.servicename.all()
service_list = Service.objects.filter(id=i.id)
print i.hostname
for x in s2:
print x.service_name
c['service'] = x.service_name
return list_detail.object_list(
request,
queryset = Devices.objects.all(),
extra_context = {'service_list': s2},
template_name = '/data/device/templates/device.html',
)
device.html
<table class="table_fluid" cellspacing="0">
<tr>
<th>Hostname </th>
<th>IP Address</th>
<th>Model</th>
<th>OS</th>
<th>Services</th>
<th>Action</th>
</tr>
{% for p in object_list %}
<tr>
<td>{{ p.hostname }}</td>
<td>{{ p.address }}</td>
<td>{{ p.type }}</td>
<td>{{ p.os }}</td>
<td>{% for xx in service_list %}{{ xx }} {% endfor %}</td>
<td width="80"><a href="/brizo/device/edit/id/{{ p.id}}">Edit</a> <a href="/brizo/device/del/id/{{ p.id}}">Delete</a></td>
</tr> {% endfor %}
</table>
example :
1 device (brizo) have multiple services (SSH,TELNET,FTP)(A)
1 device (testing) have multiple services (SSH,TELNET)(B)