2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 20:16:44 +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

@ -0,0 +1,44 @@
# Generated by Django 2.2.9 on 2020-02-15 22:32
from django.db import migrations, models
import django.db.models.deletion
import mptt.fields
class Migration(migrations.Migration):
dependencies = [
('stock', '0020_auto_20200206_1213'),
]
operations = [
migrations.AddField(
model_name='stockitem',
name='level',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='stockitem',
name='lft',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='stockitem',
name='parent',
field=mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='children', to='stock.StockItem'),
),
migrations.AddField(
model_name='stockitem',
name='rght',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='stockitem',
name='tree_id',
field=models.PositiveIntegerField(db_index=True, default=0, editable=False),
preserve_default=False,
),
]

View File

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

View File

@ -57,6 +57,11 @@
{% trans "This stock item will be automatically deleted when all stock is depleted." %} {% trans "This stock item will be automatically deleted when all stock is depleted." %}
</div> </div>
{% endif %} {% endif %}
{% if item.parent %}
<div class='alert alert-block alert-info'>
{% trans "This stock item has been split from " %}<a href="{% url 'stock-item-detail' item.parent.id %}">{{ item.parent }}</a>
</div>
{% endif %}
</div> </div>
<div class='row'> <div class='row'>