2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-03 04:00:57 +00:00

Add 'parent' field for StockItem

- Allows StockItem to be tracked when it is split into multiple items
- Uses MPTT field
This commit is contained in:
Oliver Walters
2020-02-17 21:52:31 +11:00
parent f03f6c4386
commit 750dfcda07
3 changed files with 59 additions and 3 deletions

View File

@ -18,7 +18,7 @@ from django.dispatch import receiver
from markdownx.models import MarkdownxField
from mptt.models import TreeForeignKey
from mptt.models import MPTTModel, TreeForeignKey
from decimal import Decimal, InvalidOperation
from datetime import datetime
@ -102,11 +102,12 @@ def before_delete_stock_location(sender, instance, using, **kwargs):
child.save()
class StockItem(models.Model):
class StockItem(MPTTModel):
"""
A StockItem object represents a quantity of physical instances of a part.
Attributes:
parent: Link to another StockItem from which this StockItem was created
part: Link to the master abstract part that this StockItem is an instance of
supplier_part: Link to a specific SupplierPart (optional)
location: Where this StockItem is located
@ -296,6 +297,11 @@ class StockItem(models.Model):
}
)
parent = TreeForeignKey('self',
on_delete=models.DO_NOTHING,
blank=True, null=True,
related_name='children')
part = models.ForeignKey('part.Part', on_delete=models.CASCADE,
related_name='stock_items', help_text=_('Base part'),
limit_choices_to={
@ -530,6 +536,7 @@ class StockItem(models.Model):
# Nullify the PK so a new record is created
new_stock = StockItem.objects.get(pk=self.pk)
new_stock.pk = None
new_stock.parent = self
new_stock.quantity = quantity
new_stock.save()