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
--
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...@googlegroups.com.
To post to this group, send email to django...@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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMKMUjuRrwxkKGtNEKDibHMaMm4pUEP_YX2rvxZcCGJzPfDxng%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAH-SnCCTtxDzU5_N2uFcZR12nxnE-_Hk7OWVsNmxCaPBxb2e-Q%40mail.gmail.com.
Why do you need to do any calculations in the model code?
I would do any calculations in the views and store them using models. That will remove the getters and setters from model.
Everything should work fine then.Hope it helps.
--
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...@googlegroups.com.
To post to this group, send email to django...@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/ed1853bb-402a-4688-9061-63054c44ce63%40googlegroups.com.