Add "pretty" name to the SupplierPart API

- Quick way to ensure the supplier part objects are printed the same way
- Use the pythonic back-end to render the data
This commit is contained in:
Oliver Walters 2020-08-26 23:23:57 +10:00
parent 5aa11063a0
commit 3a75682878
3 changed files with 17 additions and 0 deletions

View File

@ -158,6 +158,11 @@ class SupplierPartList(generics.ListCreateAPIView):
kwargs['manufacturer_detail'] = str2bool(self.request.query_params.get('manufacturer_detail', None))
except AttributeError:
pass
try:
kwargs['pretty'] = str2bool(self.request.query_params.get('pretty', None))
except AttributeError:
pass
kwargs['context'] = self.get_serializer_context()

View File

@ -418,6 +418,10 @@ class SupplierPart(models.Model):
return [line.order for line in self.purchase_order_line_items.all().prefetch_related('order')]
@property
def pretty_name(self):
return str(self)
def __str__(self):
s = "{supplier} ({sku})".format(
sku=self.SKU,

View File

@ -80,13 +80,17 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True)
manufacturer_detail = CompanyBriefSerializer(source='manufacturer', many=False, read_only=True)
pretty_name = serializers.CharField(read_only=True)
def __init__(self, *args, **kwargs):
part_detail = kwargs.pop('part_detail', False)
supplier_detail = kwargs.pop('supplier_detail', False)
manufacturer_detail = kwargs.pop('manufacturer_detail', False)
prettify = kwargs.pop('pretty', False)
super(SupplierPartSerializer, self).__init__(*args, **kwargs)
@ -99,6 +103,9 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
if manufacturer_detail is not True:
self.fields.pop('manufacturer_detail')
if prettify is not True:
self.fields.pop('pretty_name')
supplier = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_supplier=True))
manufacturer = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_manufacturer=True))
@ -109,6 +116,7 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
'pk',
'part',
'part_detail',
'pretty_name',
'supplier',
'supplier_detail',
'SKU',