当使用schedule_files删除用户时,我得到了这个:
/ admin / auth / user / 166 / delete /中的IntegrityError (1451,'无法删除或更新父行:外键约束失败(
vacation
。vacations_schedulefile
,CONSTRAINTvacations_schedulefile_user_id_e85fa52f_fk_auth_user_id
FOREIGN KEY(user_id
)参考{{1} }(auth_user
))')
我的模特是:
id
和
class ScheduleFile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
added = models.DateTimeField(default=django.utils.timezone.now)
def __str__(self):
return '[User ID: {}] {}'.format(str(self.user.id), self.user.username)
和用户(AbstractUser) 我决定创建pre_delete信号并删除调度模型的相关对象:
class VacationEvent(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
schedule_type = models.CharField(choices=SCHEDULE_TYPES, default=STRICT_TIME, max_length=3)
exclude_fba_from_chatzos = models.BooleanField(default=False)
schedule_file = models.ForeignKey(ScheduleFile, on_delete=models.CASCADE, null=True)
status = models.CharField(verbose_name="status event", choices=EVENT_STATUSES, max_length=3, default=CREATED)
但它返回'NoneType'对象没有属性'delete'。我该怎么办?谢谢你的帮助。
答案 0 :(得分:1)
你无法删除ScheduleFile,因为它的主键是requerid VacationEvent所以你必须删除之前的VacationEvent记录 ScheduleFile模型可以通过信号或覆盖以servral方式完成 删除ScheduleFile模型上的函数
示例:
def delete(self, using=None):
if self.schedule_file:
self.schedule_file.delete()
super(ScheduleFile, self).delete(using)