0

I'm trying to create an identificator for my Scheduler model which depends on ManyToManyField of this model.

The problem is that when I override save method, the first time (when object is created) it causes problems. It should be saved first. On the other hand when I create a post_save signal, the problem is that I have to save the model inside this signal which ends with infi

class Scheduler(models.Model):
    weekhours = models.ManyToManyField('WeekHour', related_name='schedulers')
    identificator = models.TextField(null=True,blank=True)

    def save(self,*args,**kwargs):
        if self.weekhours.all():
            identificator = ','.join([str(x.hour) for x in self.weekhours.all().order_by('hour')])
            self.identificator = identificator
        super(Scheduler, self).save(*args, **kwargs)

ValueError: "<Scheduler: None>" needs to have a value for field "scheduler" before this many-to-many relationship can be used.

Do you have any ideas?

Milano
  • 18,048
  • 37
  • 153
  • 353

1 Answers1

0

Hm, what about

# some logic here (count the identificator)
objects.filter(id=my_id).update(identificator=identificator)

in your signal - and do not override the save? :)

opalczynski
  • 1,599
  • 12
  • 14