信号Post_Save和Pre_Save出错:创建并自我!怎么修?

时间:2017-05-02 11:17:28

标签: django django-models django-signals

  

有必要在添加系列之后,数量   如果管理员忘记,系列会自动添加   以这种方式添加:我们从那里采取最后创建的系列   系列号,并添加到系列1的编号,并添加   到我们的系列!但不断vylazyut这样的错误:

1)缺乏

  

论证"自我",添加它(虽然它为什么存在,但事实并非如此   已知)仍然无法正常工作!

这是我的模型和SIGNALS

class Series(models.Model):
    id                                          = models.AutoField(primary_key=True)
    rus_name                                    = models.CharField(max_length=60) 
    eng_name                                    = models.CharField(max_length=60) 
    slug                                        = models.SlugField(unique=False) 
    serial_of_this_series                       = models.ForeignKey(Serial, on_delete=models.CASCADE, default=True)
    season_of_this_series                       = models.ForeignKey(Season, on_delete=models.CASCADE, default=True)
    number_of_series                            = models.IntegerField(default=0, blank=True, null=True)
    description                                 = models.TextField(max_length=700, blank=True, default=None)
    size_of_torent_file                         = models.CharField(max_length=60, default=None)
    link_for_dowloand_serie_in_quality_360p     = models.CharField(max_length=60, default=None)      
    link_for_dowloand_serie_in_quality_720p     = models.CharField(max_length=60, default=None)      
    link_for_dowloand_serie_in_quality_1080p    = models.CharField(max_length=60, default=None)      
    rating                                      = models.FloatField(default=0, blank=True)  
    is_active                                   = models.BooleanField(default=True)
    timestamp_rus                               = models.DateField(auto_now_add=True, auto_now=False)
    updated                                     = models.DateTimeField(auto_now_add=False, auto_now=True)
    timestamp_eng                               = models.CharField(max_length=60) 
    time_of_series                              = models.DecimalField(max_digits=10, decimal_places=2, default=42)



    def get_absolute_url(self):
        return reverse('series:post_of_serie', kwargs=
                      {'serial_slug': self.serial_of_this_series.slug,
                       'season_slug': self.season_of_this_series.slug,
                       'series_slug': self.slug})

    def __str__(self):
        return "%s | %s" % (self.rus_name, self.number_of_series)


    class Meta:
        ordering                                = ["-timestamp_rus"]
        verbose_name                            = 'Series'
        verbose_name_plural                     = 'Series'


def series_change_number(sender, **kwargs):
    ser                                         = Series.objects.last()
    change                                      = ser.number_of_series
    number                                      = int(change) + 1
    series                                      = Series
    series.number_of_series                     = number
    series.save(force_update=True)

pre_save.connect(series_change_number, sender=Series)

2 个答案:

答案 0 :(得分:1)

确定这样做:

def series_change_number(sender, instance, **kwargs):
    ser = Series.objects.last()
    change = ser.number_of_series
    number = int(change) + 1
    instance.number_of_series = number

pre_save.connect(series_change_number, sender=Series)

如果您要更新新的模型对象。

答案 1 :(得分:0)

请不要那样排列你的代码;这让人很难阅读。

你的问题在这里(删除空格):

series = Series

这只是使series成为Series类的另一个名称。你永远不会实例化它;这样做你需要实际调用它。

series = Series()

...假设这实际上是你想要做的;你的代码不清楚。