Django post_save信号,创建参数

时间:2016-08-24 14:45:26

标签: django django-models django-signals

我无法从我的created信号接收器访问post_save参数。

@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()

当我在save()个实例上调用Facility时,收到以下错误:

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

0 个答案:

没有答案