"""回填订单 ClubID(空值设为 xq)。""" from django.core.management.base import BaseCommand from django.db import transaction from jituan.constants import CLUB_ID_DEFAULT from orders.models import Order class Command(BaseCommand): help = '回填 Order.ClubID:空或缺省设为 xq' def add_arguments(self, parser): parser.add_argument( '--dry-run', action='store_true', help='仅统计不落库', ) def handle(self, *args, **options): dry_run = options.get('dry_run', False) updated = 0 skipped = 0 for order in Order.query.all().only('id', 'OrderID', 'ClubID'): current = getattr(order, 'ClubID', None) or '' if current: skipped += 1 continue if dry_run: self.stdout.write(f'[dry-run] {order.OrderID} -> ClubID={CLUB_ID_DEFAULT}') updated += 1 continue with transaction.atomic(): order.ClubID = CLUB_ID_DEFAULT order.save(update_fields=['ClubID']) updated += 1 suffix = '(dry-run)' if dry_run else '' self.stdout.write(self.style.SUCCESS(f'完成{suffix}:更新 {updated},跳过 {skipped}'))