0

I'm having trouble accessing the created argument, from my post_save signal receiver.

@receiver(post_save, sender=Facility)
def add_times(sender, **kwargs):
    instance = kwargs.get('instance', None)
    created = kwargs.get('created', False)

    if isinstance(instance, Facility) and created:
        instance.add_opening_times()

When I call save() on a Facility instance, I receive the following error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\bin\Python35\lib\site-packages\django\db\models\base.py", line 708, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\bin\Python35\lib\site-packages\django\db\models\base.py", line 745, in save_base
    update_fields=update_fields, raw=raw, using=using)
  File "C:\bin\Python35\lib\site-packages\django\dispatch\dispatcher.py", line 192, in send
    response = receiver(signal=self, sender=sender, **named)
  File "C:\git\SimplySport\facility\models.py", line 53, in update_stock
    instance = kwargs.get('instance', None)
NameError: name 'created' is not defined

https://docs.djangoproject.com/en/1.9/ref/signals/#post-save

Juddling
  • 4,594
  • 8
  • 34
  • 40
  • 1
    Did you restart your shell after changing the code? – knbk Aug 24 '16 at 15:10
  • Oh my word, no. I just re-imported the module. Problem solved, this works, thanks! – Juddling Aug 24 '16 at 15:21
  • 1
    Imported modules are cached into `sys.modules`. In this case, I saw that the traceback didn't match the error message. Since the traceback is reconstructed from the source code files, this usually means that the source code files have changed after the code was compiled and loaded. – knbk Aug 24 '16 at 15:31

0 Answers0