Add "Reference" field to Build model

This commit is contained in:
Oliver Walters 2020-10-19 22:36:14 +11:00
parent 416cfb99da
commit 934078a42c
2 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,64 @@
# Generated by Django 3.0.7 on 2020-10-19 11:25
from django.db import migrations, models
def add_default_reference(apps, schema_editor):
"""
Add a "default" build-order reference for any existing build orders.
Best we can do is use the PK of the build order itself.
"""
Build = apps.get_model('build', 'Build')
count = 0
for build in Build.objects.all():
build.reference = str(build.pk)
build.save()
count += 1
print(f"\nUpdated build reference for {count} existing BuildOrder objects")
def reverse_default_reference(apps, schema_editor):
"""
Do nothing! But we need to have a function here so the whole process is reversible.
"""
pass
class Migration(migrations.Migration):
dependencies = [
('build', '0017_auto_20200426_0612'),
]
operations = [
# Initial operation - create a 'reference' field for the Build object:
migrations.AddField(
model_name='build',
name='reference',
field=models.CharField(help_text='Build Order Reference', blank=True, max_length=64, unique=False, verbose_name='Reference'),
),
# Auto-populate the new reference field for any existing build order objects
migrations.RunPython(
add_default_reference,
reverse_code=reverse_default_reference
),
# Now that each build has a non-empty, unique reference, update the field requirements!
migrations.AlterField(
model_name='build',
name='reference',
field=models.CharField(
help_text='Build Order Reference',
max_length=64,
blank=False,
unique=True,
verbose_name='Reference'
)
)
]

View File

@ -34,6 +34,7 @@ class Build(MPTTModel):
Attributes: Attributes:
part: The part to be built (from component BOM items) part: The part to be built (from component BOM items)
reference: Build order reference (required, must be unique)
title: Brief title describing the build (required) title: Brief title describing the build (required)
quantity: Number of units to be built quantity: Number of units to be built
parent: Reference to a Build object for which this Build is required parent: Reference to a Build object for which this Build is required
@ -69,6 +70,14 @@ class Build(MPTTModel):
except PartModels.Part.DoesNotExist: except PartModels.Part.DoesNotExist:
pass pass
reference = models.CharField(
unique=True,
max_length=64,
blank=False,
help_text=_('Build Order Reference'),
verbose_name=_('Reference'),
)
title = models.CharField( title = models.CharField(
verbose_name=_('Build Title'), verbose_name=_('Build Title'),
blank=False, blank=False,