mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 12:06:44 +00:00
Fixed serializer
This commit is contained in:
parent
8c8b25a0d2
commit
a8b858c824
@ -204,7 +204,7 @@ class Company(models.Model):
|
|||||||
@property
|
@property
|
||||||
def parts(self):
|
def parts(self):
|
||||||
""" Return SupplierPart objects which are supplied or manufactured by this company """
|
""" 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
|
@property
|
||||||
def part_count(self):
|
def part_count(self):
|
||||||
|
@ -158,11 +158,9 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
|
|||||||
|
|
||||||
supplier = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_supplier=True))
|
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')
|
||||||
|
|
||||||
MPN = serializers.StringRelatedField(source='manufacturer_part.MPN', read_only=True)
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = SupplierPart
|
model = SupplierPart
|
||||||
@ -174,7 +172,6 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
|
|||||||
'supplier',
|
'supplier',
|
||||||
'supplier_detail',
|
'supplier_detail',
|
||||||
'SKU',
|
'SKU',
|
||||||
# 'manufacturer_part',
|
|
||||||
'manufacturer',
|
'manufacturer',
|
||||||
'MPN',
|
'MPN',
|
||||||
'manufacturer_detail',
|
'manufacturer_detail',
|
||||||
@ -185,30 +182,32 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
|
|||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
""" Extract manufacturer data and process ManufacturerPart """
|
""" Extract manufacturer data and process ManufacturerPart """
|
||||||
|
|
||||||
print(validated_data)
|
|
||||||
|
|
||||||
# Create SupplierPart
|
# Create SupplierPart
|
||||||
supplier_part = super().create(validated_data)
|
supplier_part = super().create(validated_data)
|
||||||
|
|
||||||
# Get ManufacturerPart data
|
# Get ManufacturerPart raw data (unvalidated)
|
||||||
part = validated_data.get('part', None)
|
manufacturer_id = self.initial_data.get('manufacturer', None)
|
||||||
manufacturer = validated_data.get('manufacturer', None)
|
MPN = self.initial_data.get('MPN', None)
|
||||||
MPN = validated_data.get('MPN', None)
|
|
||||||
description = validated_data.get('description', None)
|
|
||||||
link = validated_data.get('link', 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
|
# Create ManufacturerPart
|
||||||
manufacturer_part = ManufacturerPart.create(part=part,
|
manufacturer_part = ManufacturerPart.create(part=part,
|
||||||
manufacturer=manufacturer,
|
manufacturer=manufacturer,
|
||||||
mpn=MPN,
|
mpn=MPN,
|
||||||
description=description,
|
description=description,
|
||||||
link=link)
|
link=link)
|
||||||
print(manufacturer_part)
|
|
||||||
supplier_part.manufacturer_part = manufacturer_part
|
supplier_part.manufacturer_part = manufacturer_part
|
||||||
print(supplier_part.manufacturer_part)
|
|
||||||
supplier_part.save()
|
supplier_part.save()
|
||||||
|
|
||||||
return supplier_part
|
return supplier_part
|
||||||
|
Loading…
x
Reference in New Issue
Block a user