from django.db import transaction
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import UserAddressModel

@receiver(post_save, sender=UserAddressModel)
def ensure_single_main_address(sender, instance, **kwargs):
    if instance.is_main:
        with transaction.atomic():
            UserAddressModel.objects.filter(user=instance.user).exclude(pk=instance.pk).update(is_main=False)