2016-12-11 57 views
0

我正在嘗試爲我的調度程序模型創建標識符,該模型取決於此模型的ManyToManyField當創建對象時,覆蓋保存方法會引發異常

問題是,當我重寫save方法時,第一次(當創建對象時)會導致問題。它應該先保存。就當我創建post_save信號另一方面,問題是,我必須save這個信號與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.

你有什麼想法結束裏面的模型?

回答

0

嗯,怎麼樣

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

在你的信號 - 不要覆蓋保存? :)

相關問題