मैं Django में एक संकेत कॉलबैक है:Django अस्थायी रूप से अक्षम संकेतों
@receiver(post_save, sender=MediumCategory)
def update_category_descendants(sender, **kwargs):
def children_for(category):
return MediumCategory.objects.filter(parent=category)
def do_update_descendants(category):
children = children_for(category)
descendants = list() + list(children)
for descendants_part in [do_update_descendants(child) for child in children]:
descendants += descendants_part
category.descendants.clear()
for descendant in descendants:
if category and not (descendant in category.descendants.all()):
category.descendants.add(descendant)
category.save()
return list(descendants)
# call it for update
do_update_descendants(None)
लेकिन समारोह शरीर में मैं मॉडल MediumCategory
couses कि कि संकेत फिर से भेजा जाता है पर .save()
उपयोग कर रहा हूँ। मैं इसे कैसे अक्षम कर सकता हूं; सही समाधान with
कथन के अंदर कुछ 'जादू' के साथ होगा।
अद्यतन: यदि कोई दिलचस्पी लेता है तो यह अंतिम समाधान है।
class MediumCategory(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(blank=True)
parent = models.ForeignKey('self', blank=True, null=True)
parameters = models.ManyToManyField(AdvertisementDescriptonParameter, blank=True)
count_mediums = models.PositiveIntegerField(default=0)
count_ads = models.PositiveIntegerField(default=0)
descendants = models.ManyToManyField('self', blank=True, null=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(MediumCategory, self).save(*args, **kwargs)
def __unicode__(self):
return unicode(self.name)
(...)
@receiver(post_save, sender=MediumCategory)
def update_category_descendants(sender=None, **kwargs):
def children_for(category):
return MediumCategory.objects.filter(parent=category)
def do_update_descendants(category):
children = children_for(category)
descendants = list() + list(children)
for descendants_part in [do_update_descendants(child) for child in children]:
descendants += descendants_part
if category:
category.descendants.clear()
for descendant in descendants:
category.descendants.add(descendant)
return list(descendants)
# call it for update
do_update_descendants(None)
ठीक है, मैं यही देख रहा था: 'डिस्कनेक्ट' समाधान है, इसे 'कथन' में डालकर शुद्धता का मामला है :) लेकिन 'save() 'को हटाने के बाद, डिस्कनेक्ट की आवश्यकता नहीं है। उत्तम। – bartek
आप सही हैं, 'save()' की आवश्यकता नहीं है। – bartek