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>
<#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>
<#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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to