That approach is not working for the original poster. So I gave him a solution that solves the problem.
I meant, its my opinion to keep models slim and write utils to make the controllers slim. You don't have to follow it ;-) Follow what makes sense to you. I prefer it that way. Regards, Chetan Ganji +91-900-483-4183 ganji.che...@gmail.com http://ryucoder.in <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> I’m protected online with Avast Free Antivirus. Get it here — it’s free forever. <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> On Thu, Mar 14, 2019 at 9:01 PM Sithembewena L. Dube <zebr...@gmail.com> wrote: > There is nothing wrong with having logic in models. > > This is the principle of object orientation - encapsulating methods and > properties in a class definition to group related behaviours and attributes > of a specified type of entity. > > See the section titled "Make ‘em Fat" here: > https://django-best-practices.readthedocs.io/en/latest/applications.html > > > Kind regards, > Sithembewena > > > *Sent with Shift > <https://tryshift.com/?utm_source=SentWithShift&utm_campaign=Sent%20with%20Shift%20Signature&utm_medium=Email%20Signature&utm_content=General%20Email%20Group>* > > On Thu, Mar 14, 2019 at 5:21 PM Chetan Ganji <ganji.che...@gmail.com> > wrote: > >> 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. >> >> >> Regards, >> Chetan Ganji >> +91-900-483-4183 >> ganji.che...@gmail.com >> http://ryucoder.in >> >> >> >> <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> >> I’m >> protected online with Avast Free Antivirus. Get it here — it’s free >> forever. >> <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> >> <#m_3800371179043208066_m_7932060366693994324_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> >> >> On Thu, Mar 14, 2019 at 8:31 PM mccc <mcicc...@gmail.com> wrote: >> >>> 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 >>> <https://groups.google.com/d/msgid/django-users/292cfb9d-c605-46ef-bb4a-5d69a8ecbb6b%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >> <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> >> I’m >> protected online with Avast Free Antivirus. Get it here — it’s free >> forever. >> <https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=7ce9710f-d0c2-48a2-a0ba-76107578348e> >> <#m_3800371179043208066_m_7932060366693994324_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> >> >> -- >> 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/CAMKMUjuRrwxkKGtNEKDibHMaMm4pUEP_YX2rvxZcCGJzPfDxng%40mail.gmail.com >> <https://groups.google.com/d/msgid/django-users/CAMKMUjuRrwxkKGtNEKDibHMaMm4pUEP_YX2rvxZcCGJzPfDxng%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- > 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/CAH-SnCCTtxDzU5_N2uFcZR12nxnE-_Hk7OWVsNmxCaPBxb2e-Q%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAH-SnCCTtxDzU5_N2uFcZR12nxnE-_Hk7OWVsNmxCaPBxb2e-Q%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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/CAMKMUjv1qSdsGic1f0uqyPvLpEf%2BCEuFT8QnYpY9YXXwckK%3DNw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.