2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-03 20:20:58 +00:00
Files
InvenTree/InvenTree/stock/migrations/0081_auto_20220801_0044.py
Oliver 175d9555b0 Tree query improvements (#3443)
* Allow part category table to be ordered by part count

* Add queryset annotation for part-category part-count

- Uses subquery to annotate the part-count for sub-categories
- Huge reduction in number of queries

* Update 'pathstring' property of PartCategory and StockLocation

- No longer a dynamically calculated value
- Constructed when the model is saved, and then written to the database
- Limited to 250 characters

* Data migration to re-construct pathstring for PartCategory objects

* Fix for tree model save() method

* Add unit tests for pathstring construction

* Data migration for StockLocation pathstring values

* Update part API

- Add new annotation to PartLocationDetail view

* Update API version

* Apply similar annotation to StockLocation API endpoints

* Extra tests for PartCategory API

* Unit test fixes

* Allow PartCategory and StockLocation lists to be sorted by 'pathstring'

* Further unit test fixes
2022-08-01 13:43:27 +10:00

56 lines
1.2 KiB
Python

# Generated by Django 3.2.14 on 2022-08-01 00:44
from django.db import migrations
from InvenTree.helpers import constructPathString
def update_pathstring(apps, schema_editor):
"""Construct pathstring for all existing StockLocation objects"""
StockLocation = apps.get_model('stock', 'stocklocation')
n = StockLocation.objects.count()
if n > 0:
for loc in StockLocation.objects.all():
# Construct complete path for category
path = [loc.name]
parent = loc.parent
# Iterate up the tree
while parent is not None:
path = [parent.name] + path
parent = parent.parent
pathstring = constructPathString(path)
loc.pathstring = pathstring
loc.save()
print(f"\n--- Updated 'pathstring' for {n} StockLocation objects ---\n")
def nupdate_pathstring(apps, schema_editor):
"""Empty function for reverse migration compatibility"""
pass
class Migration(migrations.Migration):
dependencies = [
('stock', '0080_stocklocation_pathstring'),
]
operations = [
migrations.RunPython(
update_pathstring,
reverse_code=nupdate_pathstring
)
]