diff --git a/apps/collection/migrations/0028_guide_initial_objects_counter.py b/apps/collection/migrations/0028_guide_initial_objects_counter.py index 21f6649c..cfd77af7 100644 --- a/apps/collection/migrations/0028_guide_initial_objects_counter.py +++ b/apps/collection/migrations/0028_guide_initial_objects_counter.py @@ -12,7 +12,7 @@ class Migration(migrations.Migration): operations = [ migrations.AddField( model_name='guide', - name='count_objects_during_init', + name='count_related_objects', field=models.PositiveIntegerField(default=0, help_text='* after rebuild guide, refresh count of related guide elements', verbose_name='count of related guide elements during initialization'), ), ] diff --git a/apps/collection/models.py b/apps/collection/models.py index 4b063388..457d3636 100644 --- a/apps/collection/models.py +++ b/apps/collection/models.py @@ -271,7 +271,7 @@ class Guide(ProjectBaseMixin, CollectionNameMixin, CollectionDateMixin): verbose_name=_('site settings')) state = models.PositiveSmallIntegerField(default=WAITING, choices=STATE_CHOICES, verbose_name=_('state')) - count_objects_during_init = models.PositiveIntegerField( + count_related_objects = models.PositiveIntegerField( default=0, help_text=_('* after rebuild guide, refresh count of related guide elements'), verbose_name=_('count of related guide elements during initialization')) @@ -328,6 +328,16 @@ class Guide(ProjectBaseMixin, CollectionNameMixin, CollectionDateMixin): # parent_id__isnull=True).count() # return counter + def update_count_related_objects(self, nodes: set = ('EstablishmentNode', 'WineNode')): + """Update count of related guide element objects.""" + descendants = GuideElement.objects.get_root_node(self) \ + .get_descendants() + if descendants: + updated_count = descendants.filter( + guide_element_type__name__in=nodes).count() + self.count_related_objects = updated_count + self.save() + class AdvertorialQuerySet(models.QuerySet): """QuerySet for model Advertorial.""" diff --git a/apps/collection/transfer_data.py b/apps/collection/transfer_data.py index 5e796b4c..d35e8c9e 100644 --- a/apps/collection/transfer_data.py +++ b/apps/collection/transfer_data.py @@ -254,12 +254,12 @@ def transfer_guide_elements_bulk(): for guide in tqdm(Guide.objects.all(), desc='update count_of_initial_objects field values'): - count_of_initial_objects = GuideElement.objects.get_root_node(guide) \ - .get_descendants() \ - .filter(guide_element_type__name__in=['EstablishmentNode', 'WineNode']) \ - .count() - guide.count_objects_during_init = count_of_initial_objects - objects_to_update.append(guide) + descendants = GuideElement.objects.get_root_node(guide).get_descendants() + if descendants: + count_of_initial_objects = descendants.filter( + guide_element_type__name__in=['EstablishmentNode', 'WineNode']).count() + guide.count_related_objects = count_of_initial_objects + objects_to_update.append(guide) # update count_of_initial_objects field values Guide.objects.bulk_update(objects_to_update, ['count_objects_during_init', ])