user7871435
user7871435

Reputation:

Error with Signals Post_Save and Pre_Save: created and self! how to fix?

It is necessary that after the addition of the series, the number of the series is automatically added, if the administrator forgets to add, in this way: we take the last created series, from there we take the series number, and add to this the number of the series 1, and add to our series! But constantly vylazyut such errors as:

1) lacks the

argument "self", add it (although why it is there at all, it is not known) and still does not work!

this is my models and 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)

Upvotes: 1

Views: 1190

Answers (2)

Ajay Gupta
Ajay Gupta

Reputation: 1285

ok do this:

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)

provided you are looking to update the new model object.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599490

Please don't line up your code like that; it makes it very hard to read.

Your problem is here (removing the spaces):

series = Series

That just makes series another name for the Series class. You don't ever instantiate it; to do so you need to actually call it.

series = Series()

... assuming that is actually what you want to do; it's not clear from your code.

Upvotes: 0

Related Questions