I'm trying to write a Django custom command that will serve as a daemon for interacting with hardware physically connected to the server. For obvious reasons, I don't want to run hardware-related commands in my Django views; instead, I would rather have the views only interact with models and have the daemon listen for Django post_save signals from the model.
For testing, I have this custom command:
from django.core.management.base import BaseCommand
from django.core.management.base import CommandError
from thermostat.models import Relay
from thermostat.models import Sensor
from thermostat.models import Thermostat
from django.db.models.signals import post_save
import time
class Command(BaseCommand):
def handle(self, *args, **options):
post_save.connect(self.saved)
t = Thermostat.objects.get()
t.save()
time.sleep(30)
def saved(self, sender, **kwargs):
self.stdout.write(str(sender))
self.stdout.write(str(kwargs))
The first .save() method is recognized and the expected text is written to the console's stdout. However, it doesn't seem to receive any signals when interacting with the app in the browser or when manually saving instances in the ./manage.py shell
CLI.
What am I missing?