Hello,

I have set up this nice model:
class CustomGroup(models.Model):

    name = models.CharField(max_length=255)
    _parent = models.ForeignKey(
        "self", on_delete=models.CASCADE, null=True, 
related_name="descendants", db_column="parent"
    )
    _depth = models.IntegerField(default=1)

    @property
    def parent(self):
        return self._parent

    @property
    def depth(self):
        return self._depth

    @parent.setter
    def parent(self, value):
        if self == value:
            raise ValueError("parent must be different from self")
        p = value
        while value is not None:
            if self == value.parent:
                raise ValueError("parent cannot be a descendant")
            value = value.parent
        self._parent = p
        self.depth = (p.depth + 1) if p is not None else 1

    @depth.setter
    def depth(self, value):
        if value > MAX_GROUPS_DEPTH:
            raise ValueError("Too many nested groups")

        for descendant in self.descendants.all():
            descendant.depth = value + 1
            descendant.save()
        self._depth = value


and the CASCADE function is not getting fired when the parent gets deleted;
I tried adding another field without the property modifiers, and that 
worked as expected.

Is there any way I can have both my custom getter and setter, and the 
on_delete behaviour?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/292cfb9d-c605-46ef-bb4a-5d69a8ecbb6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to