def get_last_serail_num(self): date_created = as_current_tz(self.date_created) date_prefix = date_created.strftime('%Y%m%d') ticket = Ticket.all().select_for_update().filter( serial_num__startswith=date_prefix).order_by( '-date_created').first() if ticket: # 202212010001 num_str = ticket.serial_num[8:] num = int(num_str) return num return None
def fill_ticket_serial_number(apps, schema_editor): Ticket = apps.get_model('tickets', 'Ticket') tickets = Ticket.objects.all().order_by('date_created') curr_day = '00000000' curr_num = 1 print(f'\nFill ticket serial number ... ', end='') for ticket in tickets: # 跑这个脚本的时候,所有 ticket.serial_num == null date_created = as_current_tz(ticket.date_created) date_str = date_created.strftime('%Y%m%d') if date_str != curr_day: curr_day = date_str curr_num = 1 ticket.serial_num = curr_day + '%04d' % curr_num curr_num += 1 Ticket.objects.bulk_update(tickets, fields=('serial_num', )) print(len(tickets), end='')
def get_serial_num_date(self): date_created = as_current_tz(self.date_created) date = date_created.strftime('%Y%m%d') return date