Files
Django/jituan/management/commands/backfill_order_club_id.py

42 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""回填订单 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}'))