From a8b858c824c05c8bf271a516fda0911d855abebb Mon Sep 17 00:00:00 2001 From: eeintech Date: Thu, 1 Apr 2021 09:11:47 -0400 Subject: [PATCH] Fixed serializer --- InvenTree/company/models.py | 2 +- InvenTree/company/serializers.py | 33 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 1e2fe7d4b2..594d8a97ea 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -204,7 +204,7 @@ class Company(models.Model): @property def parts(self): """ Return SupplierPart objects which are supplied or manufactured by this company """ - return SupplierPart.objects.filter(Q(supplier=self.id) | Q(manufacturer=self.id)) + return SupplierPart.objects.filter(Q(supplier=self.id) | Q(manufacturer_part__manufacturer=self.id)) @property def part_count(self): diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index 513a07a71b..e3ecd625ee 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -158,11 +158,9 @@ class SupplierPartSerializer(InvenTreeModelSerializer): supplier = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_supplier=True)) - # manufacturer_part = ManufacturerPartSerializer(many=False, read_only=True) + manufacturer = serializers.PrimaryKeyRelatedField(source='manufacturer_part.manufacturer', read_only=True) - manufacturer = serializers.PrimaryKeyRelatedField(source='manufacturer_part.manufacturer', read_only=True) # queryset=Company.objects.filter(is_manufacturer=True)) - - MPN = serializers.StringRelatedField(source='manufacturer_part.MPN', read_only=True) + MPN = serializers.StringRelatedField(source='manufacturer_part.MPN') class Meta: model = SupplierPart @@ -174,7 +172,6 @@ class SupplierPartSerializer(InvenTreeModelSerializer): 'supplier', 'supplier_detail', 'SKU', - # 'manufacturer_part', 'manufacturer', 'MPN', 'manufacturer_detail', @@ -185,30 +182,32 @@ class SupplierPartSerializer(InvenTreeModelSerializer): def create(self, validated_data): """ Extract manufacturer data and process ManufacturerPart """ - print(validated_data) - # Create SupplierPart supplier_part = super().create(validated_data) - # Get ManufacturerPart data - part = validated_data.get('part', None) - manufacturer = validated_data.get('manufacturer', None) - MPN = validated_data.get('MPN', None) - description = validated_data.get('description', None) - link = validated_data.get('link', None) + # Get ManufacturerPart raw data (unvalidated) + manufacturer_id = self.initial_data.get('manufacturer', None) + MPN = self.initial_data.get('MPN', None) - print(f'{manufacturer} | {MPN}') + if manufacturer_id or MPN: + # Get SupplierPart data + part = validated_data.get('part', None) + description = validated_data.get('description', None) + link = validated_data.get('link', None) + + # Get manufacturer + try: + manufacturer = Company.objects.get(pk=int(manufacturer_id)) + except Company.DoesNotExist: + manufacturer = None - if manufacturer or MPN: # Create ManufacturerPart manufacturer_part = ManufacturerPart.create(part=part, manufacturer=manufacturer, mpn=MPN, description=description, link=link) - print(manufacturer_part) supplier_part.manufacturer_part = manufacturer_part - print(supplier_part.manufacturer_part) supplier_part.save() return supplier_part