From 0a78432a0fbd4a527a2f1cb095601ca56640be49 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 15 May 2020 21:35:53 +1000 Subject: [PATCH] Convert 'part" to MPTT model - based on the 'variant_of' field - Now recursive variants can be implemented properly --- .../migrations/0039_auto_20200515_1127.py | 50 +++++++++++++++++++ InvenTree/part/models.py | 12 +++-- 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 InvenTree/part/migrations/0039_auto_20200515_1127.py diff --git a/InvenTree/part/migrations/0039_auto_20200515_1127.py b/InvenTree/part/migrations/0039_auto_20200515_1127.py new file mode 100644 index 0000000000..bc25097888 --- /dev/null +++ b/InvenTree/part/migrations/0039_auto_20200515_1127.py @@ -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) + ] diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 90383ab26f..c2cfec8d9b 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -24,7 +24,7 @@ from markdownx.models import MarkdownxField from django_cleanup import cleanup -from mptt.models import TreeForeignKey +from mptt.models import TreeForeignKey, MPTTModel from stdimage.models import StdImageField @@ -200,7 +200,7 @@ def match_part_names(match, threshold=80, reverse=True, compare_length=False): @cleanup.ignore -class Part(models.Model): +class Part(MPTTModel): """ 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. @@ -236,8 +236,12 @@ class Part(models.Model): """ class Meta: - verbose_name = "Part" - verbose_name_plural = "Parts" + verbose_name = _("Part") + 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): """