mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 12:06:44 +00:00
- Saving a model automatically updates the reference_int field - Data migrations are correctly applied
67 lines
1.3 KiB
Python
67 lines
1.3 KiB
Python
# Generated by Django 3.2.5 on 2021-10-14 06:31
|
|
|
|
import re
|
|
|
|
from django.db import migrations
|
|
|
|
def build_refs(apps, schema_editor):
|
|
"""
|
|
Rebuild the integer "reference fields" for existing Build objects
|
|
"""
|
|
|
|
PurchaseOrder = apps.get_model('order', 'purchaseorder')
|
|
|
|
for order in PurchaseOrder.objects.all():
|
|
|
|
ref = 0
|
|
|
|
result = re.match(r"^(\d+)", order.reference)
|
|
|
|
if result and len(result.groups()) == 1:
|
|
try:
|
|
ref = int(result.groups()[0])
|
|
except:
|
|
ref = 0
|
|
|
|
order.reference_int = ref
|
|
order.save()
|
|
|
|
SalesOrder = apps.get_model('order', 'salesorder')
|
|
|
|
for order in SalesOrder.objects.all():
|
|
|
|
ref = 0
|
|
|
|
result = re.match(r"^(\d+)", order.reference)
|
|
|
|
if result and len(result.groups()) == 1:
|
|
try:
|
|
ref = int(result.groups()[0])
|
|
except:
|
|
ref = 0
|
|
|
|
order.reference_int = ref
|
|
order.save()
|
|
|
|
|
|
def unbuild_refs(apps, schema_editor):
|
|
"""
|
|
Provided only for reverse migration compatibility
|
|
"""
|
|
pass
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('order', '0051_auto_20211014_0623'),
|
|
]
|
|
|
|
|
|
operations = [
|
|
migrations.RunPython(
|
|
build_refs,
|
|
reverse_code=unbuild_refs
|
|
)
|
|
]
|