mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-03 12:10:59 +00:00
Data migration to initially update serial number fields for all stock items
- Also automatically updates serial_int field when saving StockItem
This commit is contained in:
55
InvenTree/stock/migrations/0069_auto_20211109_2347.py
Normal file
55
InvenTree/stock/migrations/0069_auto_20211109_2347.py
Normal file
@ -0,0 +1,55 @@
|
||||
# Generated by Django 3.2.5 on 2021-11-09 23:47
|
||||
|
||||
import re
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def update_serials(apps, schema_editor):
|
||||
"""
|
||||
Rebuild the integer serial number field for existing StockItem objects
|
||||
"""
|
||||
|
||||
StockItem = apps.get_model('stock', 'stockitem')
|
||||
|
||||
for item in StockItem.objects.all():
|
||||
|
||||
if item.serial is None:
|
||||
# Skip items without existing serial numbers
|
||||
continue
|
||||
|
||||
serial = 0
|
||||
|
||||
result = re.match(r"^(\d+)", item.serial or "")
|
||||
|
||||
if result and len(result.groups()) == 1:
|
||||
try:
|
||||
serial = int(result.groups()[0])
|
||||
except:
|
||||
serial = 0
|
||||
|
||||
|
||||
item.serial_int = serial
|
||||
print(item, item.serial, '->', item.serial_int)
|
||||
item.save()
|
||||
|
||||
|
||||
def nupdate_serials(apps, schema_editor):
|
||||
"""
|
||||
Provided only for reverse migration compatibility
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('stock', '0068_stockitem_serial_int'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
update_serials,
|
||||
reverse_code=nupdate_serials,
|
||||
)
|
||||
]
|
Reference in New Issue
Block a user