mirror of
https://github.com/inventree/InvenTree.git
synced 2025-08-29 06:45:53 +00:00
- Saving a model automatically updates the reference_int field - Data migrations are correctly applied
51 lines
981 B
Python
51 lines
981 B
Python
# Generated by Django 3.2.5 on 2021-10-14 06:32
|
|
|
|
import re
|
|
|
|
from django.db import migrations
|
|
|
|
|
|
def build_refs(apps, schema_editor):
|
|
"""
|
|
Rebuild the integer "reference fields" for existing Build objects
|
|
"""
|
|
|
|
BuildOrder = apps.get_model('build', 'build')
|
|
|
|
for build in BuildOrder.objects.all():
|
|
|
|
ref = 0
|
|
|
|
result = re.match(r"^(\d+)", build.reference)
|
|
|
|
if result and len(result.groups()) == 1:
|
|
try:
|
|
ref = int(result.groups()[0])
|
|
except:
|
|
ref = 0
|
|
|
|
build.reference_int = ref
|
|
build.save()
|
|
|
|
def unbuild_refs(apps, schema_editor):
|
|
"""
|
|
Provided only for reverse migration compatibility
|
|
"""
|
|
pass
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
atomic = False
|
|
|
|
dependencies = [
|
|
('build', '0031_build_reference_int'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(
|
|
build_refs,
|
|
reverse_code=unbuild_refs
|
|
)
|
|
]
|