from django.db.models.signals import m2m_changed
from django.dispatch import receiver

from apps.geology.models import GalleryCategory


@receiver(m2m_changed, sender=GalleryCategory.gallery.through)
def gallery_category_m2m_changed(sender, instance, action, **kwargs):
    if action in ["post_add", "post_remove", "post_clear"]:
        instance.count = instance.gallery.count()
        print(instance.count)
        instance.save()
