From 71972f4454c00df3249b3dfb55af184026dbcd95 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 4 May 2019 19:06:39 +1000 Subject: [PATCH] Add function to get the default location for a part - If field not specified, look at the part category --- InvenTree/part/models.py | 15 +++++++++++++++ InvenTree/stock/views.py | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index fb4dcd350d..f3caeeae6a 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -148,6 +148,21 @@ class Part(models.Model): help_text='Where is this item normally stored?', related_name='default_parts') + def get_default_location(self): + """ Get the default location for a Part (may be None). + + If the Part does not specify a default location, + look at the Category this part is in. + The PartCategory object may also specify a default stock location + """ + + if self.default_location: + return self.default_location + elif self.category: + return self.category.default_location + + return None + # Default supplier part default_supplier = models.ForeignKey('part.SupplierPart', on_delete=models.SET_NULL, diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index c0d1e0dc9d..fc174fa3f1 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -211,7 +211,7 @@ class StockItemCreate(AjaxCreateView): try: part = Part.objects.get(pk=part_id) initials['part'] = part - initials['location'] = part.default_location + initials['location'] = part.get_default_location() initials['supplier_part'] = part.default_supplier except Part.DoesNotExist: pass