from django.core.management.base import BaseCommand from django_tenants.utils import schema_context from tenants.models import Client from customers.models import Customer class Command(BaseCommand): help = 'Удаляет всех клиентов (кроме системного) в тенанте anatol' def handle(self, *args, **options): tenant_schema = 'anatol' try: tenant = Client.objects.get(schema_name=tenant_schema) except Client.DoesNotExist: self.stdout.write(self.style.ERROR(f'Тенант {tenant_schema} не найден')) return with schema_context(tenant_schema): # Удаляем всех клиентов кроме системного customers_to_delete = Customer.objects.filter(is_system_customer=False) count = customers_to_delete.count() if count == 0: self.stdout.write(self.style.WARNING('Нет клиентов для удаления')) return customers_to_delete.delete() # Проверяем что остался только системный remaining = Customer.objects.count() system_customer = Customer.objects.filter(is_system_customer=True).first() self.stdout.write(self.style.SUCCESS(f'Удалено клиентов: {count}')) self.stdout.write(self.style.SUCCESS(f'Осталось клиентов: {remaining}')) if system_customer: self.stdout.write(f'Системный клиент: {system_customer.name} ({system_customer.email})')