From 4f266958e3310b635b670dddfb3581584a19c8e1 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 17 Feb 2020 22:11:44 +1100 Subject: [PATCH] Add custom migration - Required to initialize the MPTT fields for the StockItem model --- InvenTree/stock/admin.py | 7 +++++++ .../migrations/0022_auto_20200217_1109.py | 21 +++++++++++++++++++ InvenTree/stock/models.py | 7 +++++++ 3 files changed, 35 insertions(+) create mode 100644 InvenTree/stock/migrations/0022_auto_20200217_1109.py diff --git a/InvenTree/stock/admin.py b/InvenTree/stock/admin.py index c33010bf3b..8c91518de0 100644 --- a/InvenTree/stock/admin.py +++ b/InvenTree/stock/admin.py @@ -85,6 +85,13 @@ class StockItemResource(ModelResource): stocktake_date = Field(attribute='stocktake_date', widget=widgets.DateWidget()) + def after_import(self, dataset, result, using_transactions, dry_run, **kwargs): + + super().after_import(dataset, result, using_transactions, dry_run, **kwargs) + + # Rebuild the StockItem tree(s) + StockItem.objects.rebuild() + class Meta: model = StockItem skip_unchanged = True diff --git a/InvenTree/stock/migrations/0022_auto_20200217_1109.py b/InvenTree/stock/migrations/0022_auto_20200217_1109.py new file mode 100644 index 0000000000..0db3985361 --- /dev/null +++ b/InvenTree/stock/migrations/0022_auto_20200217_1109.py @@ -0,0 +1,21 @@ +# Generated by Django 2.2.9 on 2020-02-17 11:09 + +from django.db import migrations +from stock import models + + +def update_stock_item_tree(apps, schema_editor): + # Update the StockItem MPTT model + + models.StockItem.objects.rebuild() + + +class Migration(migrations.Migration): + + dependencies = [ + ('stock', '0021_auto_20200215_2232'), + ] + + operations = [ + migrations.RunPython(update_stock_item_tree) + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index e9e237b779..dcea62a4bb 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -385,6 +385,13 @@ class StockItem(MPTTModel): return True + @property + def child_count(self): + """ Return the number of 'child' items associated with this StockItem. + A child item is one which has been split from this one. + """ + return self.get_descendants(include_self=False).count() + @property def in_stock(self):