From cd8214ff4a7c530440432d1196af1e25ee276263 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 31 Aug 2020 22:26:46 +1000 Subject: [PATCH 1/2] Part: add function to get part attachments for *all* parents of a part --- InvenTree/part/models.py | 14 ++++++++++++++ InvenTree/part/templates/part/attachments.html | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index e5b035f856..c5da47b33e 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1087,6 +1087,20 @@ class Part(MPTTModel): return n + @property + def part_attachments(self): + """ + Return *all* attachments for this part, + potentially including attachments for template parts + above this one. + """ + + ancestors = self.get_ancestors(include_self=True) + + attachments = PartAttachment.objects.filter(part__in=ancestors) + + return attachments + def sales_orders(self): """ Return a list of sales orders which reference this part """ diff --git a/InvenTree/part/templates/part/attachments.html b/InvenTree/part/templates/part/attachments.html index 049ef0cd7a..aa31cd3f32 100644 --- a/InvenTree/part/templates/part/attachments.html +++ b/InvenTree/part/templates/part/attachments.html @@ -9,7 +9,7 @@
-{% include "attachment_table.html" with attachments=part.attachments.all %} +{% include "attachment_table.html" with attachments=part.part_attachments %} {% endblock %} From 73892e894e064c2a30866737c763065cf1bc26e4 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 31 Aug 2020 22:30:38 +1000 Subject: [PATCH 2/2] Refactoring --- InvenTree/part/models.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index c5da47b33e..26dd04ac52 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1080,12 +1080,7 @@ class Part(MPTTModel): """ - n = self.attachments.count() - - if self.variant_of: - n += self.variant_of.attachments.count() - - return n + return self.part_attachments.count() @property def part_attachments(self):