On Sun, May 28, 2017 at 8:50 AM, pradam programmer <pradam.programming@gmail.com> wrote:
from django.db.models import QuerySetimport attrfrom attr.validators import instance_of@attr.sclass 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_leveldef 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()
This should be generalized as an abstract model (https://docs.djangoproject.com/en/1.11/topics/db/models/#abstract-base-classes) and any models with children should inherit from it.
# example
class HasChildren(models.Model):
activated = models.BooleanField(default=False)
def get_children(self):
# logic to return a list of children models
def set_activation_status(self, new_status):
# logic to deactivate this node
self.activated = new_status
self.save()
def set_children_activation_status(self, new_status):
# get list from self.get_children()
# run self.set_activation_status(new_status) on all members in list
# If every model is same type, may be more efficient to use an update query, something like
# self.__class__.objects.filter(id=[list of children]).update(activated=new_status)
class Meta:
abstract = True
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..?
Using an abstract model class and only referring to self (rather than a specific class), you can keep your code DRY and generic.
If all of the parents and children are the same model type, you may be able to take advantage of an MPTT tree (ie http://django-mptt.github.io/django-mptt/), which will allow you to maintain an ordered parent->child relationship (and even siblings if needed), while reducing the number of queries needed to pull/modify all of the parents and children in the hierarchy (in most cases, 1).
-James
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/CA%2Be%2BciURQ%2BvFPE%2Bkkc5Xcg57npuOMrJmEm2vBqHTm_VzR24dgA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment