Convert 'part" to MPTT model - based on the 'variant_of' field

- Now recursive variants can be implemented properly
This commit is contained in:
Oliver Walters 2020-05-15 21:35:53 +10:00
parent 72cfaccac5
commit 0a78432a0f
2 changed files with 58 additions and 4 deletions

View File

@ -0,0 +1,50 @@
# Generated by Django 3.0.5 on 2020-05-15 11:27
from django.db import migrations, models
from part.models import Part
def update_tree(apps, schema_editor):
# Update the MPTT for Part model
Part.objects.rebuild()
def nupdate_tree(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
('part', '0038_auto_20200513_0016'),
]
operations = [
migrations.AddField(
model_name='part',
name='level',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='part',
name='lft',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='part',
name='rght',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='part',
name='tree_id',
field=models.PositiveIntegerField(db_index=True, default=0, editable=False),
preserve_default=False,
),
migrations.RunPython(update_tree, reverse_code=nupdate_tree)
]

View File

@ -24,7 +24,7 @@ from markdownx.models import MarkdownxField
from django_cleanup import cleanup from django_cleanup import cleanup
from mptt.models import TreeForeignKey from mptt.models import TreeForeignKey, MPTTModel
from stdimage.models import StdImageField from stdimage.models import StdImageField
@ -200,7 +200,7 @@ def match_part_names(match, threshold=80, reverse=True, compare_length=False):
@cleanup.ignore @cleanup.ignore
class Part(models.Model): class Part(MPTTModel):
""" The Part object represents an abstract part, the 'concept' of an actual entity. """ The Part object represents an abstract part, the 'concept' of an actual entity.
An actual physical instance of a Part is a StockItem which is treated separately. An actual physical instance of a Part is a StockItem which is treated separately.
@ -236,8 +236,12 @@ class Part(models.Model):
""" """
class Meta: class Meta:
verbose_name = "Part" verbose_name = _("Part")
verbose_name_plural = "Parts" verbose_name_plural = _("Parts")
class MPTTMeta:
# For legacy reasons the 'variant_of' field is used to indicate the MPTT parent
parent_attr='variant_of'
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
""" """