Sunday, May 28, 2017

Deactivate the Child Objects when parent is deactivate.

from django.db.models import QuerySet
import attr
from attr.validators import instance_of

@attr.s
class ChildDeactivate(object):
    loca = attr.ib(validator=instance_of(QuerySet))

    def deactivate_child(self,l):
        get_parent = Boundary.objects.filter(parent__id=l.id)
        if get_parent:
            deact_child = map(lambda x: x.switch(),get_parent)
        else:
            l.switch()
        return l.name, l.get_active_display(), l.boundary_level

    def list_all(self):
        for i in self.loca:
            name_bound, status, level = self.deactivate_child(i)
            print '{} is change to {} status of level {}'.format(name_bound, status, level)

l = Boundary.objects.filter(parent__id=2)
s = ChildDeactivate(l)
s.list_all()

In my location models i have 9 levels, i wrote this snippet to deactivate children objects when parent is deactivated, so anything to do to make it more generic all the over models..?

--
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+unsubscribe@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/CAGGVXBMmF%3D7ityZ520qpgmcnJRHvYkWNyMLM1L%2BXH1yQ-KH6%3DA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment