django信号模块对象没有属性'post_save'

时间:2019-06-24 14:49:49

标签: python django django-models

我试图在创建“沙龙”对象时创建一个“保险柜”对象

这是 models.py

from django.db import models,signals
from django.contrib.auth.models import User

class Vault(models.Model):
    salon = models.ForeignKey(Salon,  on_delete=models.CASCADE, null=False, unique=False)
    cash = models.PositiveIntegerField()

class Salon(models.Model):
    salon_ismi = models.CharField(max_length=25,unique=False)
    adres = models.CharField(max_length=255, unique=False)
    ulke = models.CharField(max_length=25, unique=False)
    sehir = models.CharField(max_length=24, unique=False)
    kasa = models.PositiveIntegerField(blank=True, unique=False)
    class Meta:
        ordering = ('-pk',)
        verbose_name_plural = "Salonlar"

    def __unicode__(self):
        return u'%s' % self.pk
    def __str__(self):
        return self.salon_ismi
    def get_absolute_url(self):
        return reverse('kentyonetim_salon_detail', args=(self.pk,))

    def create_vault(self, sender, instance, created, **kwargs):
        """Create Vault Model for every new Salon Model."""
        if created:
            Vault.objects.create(salon=self.salon_ismi,)

signals.post_save.connect(Salon.create_vault, sender=Salon, weak=False,
                              dispatch_uid='models.create_Vault')

当我尝试运行服务器时,它给我一个错误:

  

AttributeError:模块'django.core.signals'没有属性   'post_save'

请帮帮我。将不胜感激。谢谢。

  

版本的图书馆:
Django == 2.2.2

1 个答案:

答案 0 :(得分:1)

看起来您输入的信号不正确。请尝试以下方法:

from django.db.models import signals