[Answered ]-Issue with complex signal in Django

1๐Ÿ‘

โœ…

Iโ€™m not sure that signal are relevant in your case, a simple .save() override should be sufficient.

class Project(models.Model):
    name = models.CharField(max_length=200, default="")
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, default='')
    is_shared = models.BooleanField(default=False)
    group = models.ForeignKey(Group, on_delete=models.CASCADE, null=True, blank=True, default=1)

    def save(*args, **kwargs):
        if self.pk is None:
            return super().save(*args, **kwargs)

        old_group_id = Project.objects.get(pk=pk).group_id

        super().save(*args, **kwargs)

        if old_group_id != self.group_id:
            BaseFileModel.objects.filter(spider_set__project_id=pk).update(group_id=self.group_id)
        

Note that spider_set in spider_set__project_id is the default related_name that Django generated since you did not specified one for the BaseFileModel FK in Spider

๐Ÿ‘คAlombaros

Leave a comment