我創(chuàng)建基礎(chǔ)模型并在我的所有模型中繼承它。這是我的BaseModel:class BaseModel(models.Model): create_date = models.DateTimeField(auto_now_add=True) update_date = models.DateTimeField(auto_now=True) created_by = models.ForeignKey('UserManager.User', default=1, on_delete=models.SET_DEFAULT,related_name='created_%(class)ss') updated_by = models.ForeignKey('UserManager.User', default=1, on_delete=models.SET_DEFAULT,related_name='updated_%(class)ss') class Meta: abstract = True ordering = ['create_date'] def save(self, *args, **kwargs): self.user = kwargs.pop('user', None) if self.user: if self.user.pk is None: self.created_by = self.user self.updated_by = self.user super(BaseModel, self).save(*args, **kwargs)現(xiàn)在,我想向save其中一個(gè)子模型的方法添加一些操作,如下所示:class Child(BaseModel): # Some fields go here. def save(self, *args, **kwargs): # Some operations must run here. 但是savechild model的method已經(jīng)跑不起來了!如何將子模型的保存方法與abastract=True模型的保存方法一起使用?
在Django中添加對父模型抽象保存方法保存方法的操作
繁華開滿天機(jī)
2023-02-07 14:20:11