0

I'm using Django 1.8.

Following is my signals.py:

from django.db.models.signals import post_save
from django.dispatch import receiver
from datetime import datetime
from models import Watch, LastUpdated


@receiver(post_save, sender=Watch)
def my_handler(sender, **kwargs):
    obj = LastUpdated.objects.all()
    current_datetime = datetime.now()
    if obj:
        obj.last_updated = current_datetime
    else:
        obj = LastUpdated(last_updated=current_datetime)
    obj.save()

My models.py

class LastUpdated(models.Model):
    last_updated = models.DateTimeField(auto_now=True)

class Watch(models.Model):
    brand = models.CharField(max_length=255)
    model_name = models.CharField(max_length=255)
    dial_image = models.ImageField(upload_to='watch/')
    hour_hand = models.ImageField(upload_to='watch/')
    minute_hand = models.ImageField(upload_to='watch/')
    second_hand = models.ImageField(upload_to='watch/')
    product_url = models.URLField()
    status = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)

How do I connect signal with the model?

In earlier version, we use to do post_save.connect(my_handler, sender=Watch) in models.py file, but not anymore (as per the documentattion). How do we do it now? I don't understand the documentation.

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • What isn't working? and what do you mean by "not anymore" that code is still perfectly fine in django 1.9 – Sayse Jan 21 '16 at 10:45
  • Agreed. That code still works with the current version, but what additions do I need to add to make this code working? – Praful Bagai Jan 21 '16 at 10:53
  • I don't know, you haven't said what the problem is – Sayse Jan 21 '16 at 10:53
  • How do I make this code work? How do I connect my signals(my_handler) with the model (Watch), so that whenever I save any instance of model Watch, my handler is also called . – Praful Bagai Jan 21 '16 at 10:56
  • @PrafulBagai where is your handler function defined ? It has to be in a module that's loaded at process startup. – bruno desthuilliers Jan 21 '16 at 12:00
  • This signal will work whenever the `Watch` model is saved, its already connected, and also you have to define it in `apps.py` – Bijoy Aug 22 '17 at 04:28
  • If you are still confused, you can test it with pasting the signals.py code below the `Watch` model, then you can check it works when `Watch` model is saved, `reciever` decorator defines the function to be performed and `post_save` defines when to perform. – Bijoy Aug 22 '17 at 04:32

2 Answers2

0

Make sure your signals.py is imported (read "invoked") by your application:

yourapp/init.py

import yourapp.signals

Alternatively, you could use the AppConfig.ready() method, as it recommended in the docs (section Where should this code live?).

Alex Morozov
  • 5,823
  • 24
  • 28
0

Just to show you the right example.

Create your AppConfig with something similar to this:

# my_app/apps.py


import logging

from django.apps import AppConfig


_logger = logging.getLogger(__name__)


class AppConfig(AppConfig):
    name = "my_app"
    label = "My App"

    def ready(self):
        _logger.info("'%s' completed initialisation.", self.label)
        import my_app.signals

Remember to have correctly registered your app in your settings importing the AppConfig

# settings.py
# ...
INSTALLED_APPS = (
    # ...
    'my_app.apps.AppConfig',
)

Now, if you run your localserver, you should be able to see the initialisation statement from the logs when the app is ready. If you have not the LOGGING facility configured, you can use a print instead of _logger.info.

The my_app/signals.py is the file you mentioned in your question.

Karim N Gorjux
  • 2,880
  • 22
  • 29