mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-30 20:55:42 +00:00 
			
		
		
		
	Optional part_detail and sub_part_detail for BOM API
- Significant speed boost on request - Request the relevent information at the right place
This commit is contained in:
		| @@ -22,6 +22,7 @@ from .serializers import CategorySerializer | |||||||
| from .serializers import PartStarSerializer | from .serializers import PartStarSerializer | ||||||
|  |  | ||||||
| from InvenTree.views import TreeSerializer | from InvenTree.views import TreeSerializer | ||||||
|  | from InvenTree.helpers import str2bool | ||||||
|  |  | ||||||
|  |  | ||||||
| class PartCategoryTree(TreeSerializer): | class PartCategoryTree(TreeSerializer): | ||||||
| @@ -206,6 +207,18 @@ class BomList(generics.ListCreateAPIView): | |||||||
|  |  | ||||||
|     serializer_class = BomItemSerializer |     serializer_class = BomItemSerializer | ||||||
|      |      | ||||||
|  |     def get_serializer(self, *args, **kwargs): | ||||||
|  |  | ||||||
|  |         # Do we wish to include extra detail? | ||||||
|  |         part_detail = str2bool(self.request.GET.get('part_detail', None)) | ||||||
|  |         sub_part_detail = str2bool(self.request.GET.get('sub_part_detail', None)) | ||||||
|  |  | ||||||
|  |         kwargs['part_detail'] = part_detail | ||||||
|  |         kwargs['sub_part_detail'] = sub_part_detail | ||||||
|  |  | ||||||
|  |         kwargs['context'] = self.get_serializer_context() | ||||||
|  |         return self.serializer_class(*args, **kwargs) | ||||||
|  |  | ||||||
|     def get_queryset(self): |     def get_queryset(self): | ||||||
|         queryset = BomItem.objects.all() |         queryset = BomItem.objects.all() | ||||||
|         queryset = self.get_serializer_class().setup_eager_loading(queryset) |         queryset = self.get_serializer_class().setup_eager_loading(queryset) | ||||||
|   | |||||||
| @@ -124,6 +124,21 @@ class BomItemSerializer(InvenTreeModelSerializer): | |||||||
|     sub_part_detail = PartBriefSerializer(source='sub_part', many=False, read_only=True) |     sub_part_detail = PartBriefSerializer(source='sub_part', many=False, read_only=True) | ||||||
|     price_range = serializers.CharField(read_only=True) |     price_range = serializers.CharField(read_only=True) | ||||||
|  |  | ||||||
|  |     def __init__(self, *args, **kwargs): | ||||||
|  |         # part_detail and sub_part_detail serializers are only included if requested.  | ||||||
|  |         # This saves a bunch of database requests | ||||||
|  |  | ||||||
|  |         part_detail = kwargs.pop('part_detail', False) | ||||||
|  |         sub_part_detail = kwargs.pop('sub_part_detail', False) | ||||||
|  |  | ||||||
|  |         super(BomItemSerializer, self).__init__(*args, **kwargs) | ||||||
|  |  | ||||||
|  |         if part_detail is not True: | ||||||
|  |             self.fields.pop('part_detail') | ||||||
|  |  | ||||||
|  |         if sub_part_detail is not True: | ||||||
|  |             self.fields.pop('sub_part_detail') | ||||||
|  |  | ||||||
|     @staticmethod |     @staticmethod | ||||||
|     def setup_eager_loading(queryset): |     def setup_eager_loading(queryset): | ||||||
|         queryset = queryset.prefetch_related('part') |         queryset = queryset.prefetch_related('part') | ||||||
|   | |||||||
| @@ -72,7 +72,8 @@ | |||||||
|         editable: {{ editing_enabled }}, |         editable: {{ editing_enabled }}, | ||||||
|         bom_url: "{% url 'api-bom-list' %}", |         bom_url: "{% url 'api-bom-list' %}", | ||||||
|         part_url: "{% url 'api-part-list' %}", |         part_url: "{% url 'api-part-list' %}", | ||||||
|         parent_id: {{ part.id }}  |         parent_id: {{ part.id }} , | ||||||
|  |         sub_part_detail: true, | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     {% if editing_enabled %} |     {% if editing_enabled %} | ||||||
|   | |||||||
| @@ -30,7 +30,8 @@ | |||||||
|         formatNoMatches: function() { return "{{ part.full_name }} is not used to make any other parts"; }, |         formatNoMatches: function() { return "{{ part.full_name }} is not used to make any other parts"; }, | ||||||
|         queryParams: function(p) { |         queryParams: function(p) { | ||||||
|             return { |             return { | ||||||
|                 sub_part: {{ part.id }} |                 sub_part: {{ part.id }}, | ||||||
|  |                 part_detail: true, | ||||||
|             } |             } | ||||||
|         }, |         }, | ||||||
|         columns: [ |         columns: [ | ||||||
|   | |||||||
| @@ -190,20 +190,30 @@ function loadBomTable(table, options) { | |||||||
|  |  | ||||||
|     // Configure the table (bootstrap-table) |     // Configure the table (bootstrap-table) | ||||||
|  |  | ||||||
|  |     var params = { | ||||||
|  |         part: options.parent_id, | ||||||
|  |         ordering: 'name', | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (options.part_detail) { | ||||||
|  |         params.part_detail = true; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (options.sub_part_detail) { | ||||||
|  |         params.sub_part_detail = true; | ||||||
|  |     } | ||||||
|  |      | ||||||
|     table.bootstrapTable({ |     table.bootstrapTable({ | ||||||
|         sortable: true, |         sortable: true, | ||||||
|         search: true, |         search: true, | ||||||
|         formatNoMatches: function() { return "No BOM items found"; }, |         formatNoMatches: function() { return "No BOM items found"; }, | ||||||
|         clickToSelect: true, |         clickToSelect: true, | ||||||
|         queryParams: function(p) { |         queryParams: function(p) { | ||||||
|             return { |             return params; | ||||||
|                 part: options.parent_id, |  | ||||||
|                 ordering: 'name', |  | ||||||
|         } |  | ||||||
|         }, |         }, | ||||||
|         columns: cols, |         columns: cols, | ||||||
|         url: options.bom_url |         url: options.bom_url | ||||||
| }); |     }); | ||||||
|  |  | ||||||
|     // In editing mode, attached editables to the appropriate table elements |     // In editing mode, attached editables to the appropriate table elements | ||||||
|     if (options.editable) { |     if (options.editable) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user