Reputation: 143
Purpose: I am trying to setup a small local network monitor with basic UI that pings all the devices enlisted in the db and updates the status.
Problem: save() method is not updating the DB.
My Model:
class Device(models.Model):
dev_name = models.CharField(max_length=100)
dev_ip = models.CharField(max_length=16)
def __str__(self):
return self.dev_name
def get_absolute_url(self):
return reverse('device-detail', kwargs={'pk': self.pk})
Function I wrote to ping the device and update DB:
import platform
import subprocess
from netmon.models import Device
from background_task import background
@background(schedule=60)
def start_monitor():
devices = Device.objects.all()
print(">>>>>>>>>>> In Start_monitor <<<<<<<<<<<")
for device in devices:
if (ping(device.dev_ip)):
device.status = "Alive"
else:
device.status = "Dead"
device.save(update_fields=["dev_status"])
I am a Django beginner, please let me know if you need any more details. All answers are appreciated.
Thank you
Upvotes: 1
Views: 1109
Reputation: 476557
Based on the fact that you update the dev_status
field, you need to assign the status to device.dev_status
, not :device.status
@background(schedule=60)
def start_monitor():
devices = Device.objects.all()
print('>>>>>>>>>>> In Start_monitor <<<<<<<<<<<')
for device in devices:
if (ping(device.dev_ip)):
device.dev_status = 'Alive' # ← update dev_status
else:
device.dev_status = 'Dead' # ← update dev_status
device.save(update_fields=['dev_status'])
Upvotes: 1