2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 20:16:44 +00:00

Updates for InvenTree serializer classes

- Catch and re-throw errors correctly
This commit is contained in:
Oliver 2021-08-16 10:41:26 +10:00
parent 3dcf1746e6
commit d9f29b4a70

View File

@ -167,6 +167,18 @@ class InvenTreeModelSerializer(serializers.ModelSerializer):
return self.instance return self.instance
def update(self, instance, validated_data):
"""
Catch any django ValidationError, and re-throw as a DRF ValidationError
"""
try:
instance = super().update(instance, validated_data)
except (ValidationError, DjangoValidationError) as exc:
raise ValidationError(detail=serializers.as_serializer_error(exc))
return instance
def run_validation(self, data=empty): def run_validation(self, data=empty):
""" """
Perform serializer validation. Perform serializer validation.
@ -188,7 +200,10 @@ class InvenTreeModelSerializer(serializers.ModelSerializer):
# Update instance fields # Update instance fields
for attr, value in data.items(): for attr, value in data.items():
setattr(instance, attr, value) try:
setattr(instance, attr, value)
except (ValidationError, DjangoValidationError) as exc:
raise ValidationError(detail=serializers.as_serializer_error(exc))
# Run a 'full_clean' on the model. # Run a 'full_clean' on the model.
# Note that by default, DRF does *not* perform full model validation! # Note that by default, DRF does *not* perform full model validation!
@ -219,20 +234,10 @@ class InvenTreeAttachmentSerializer(InvenTreeModelSerializer):
filename = serializers.CharField( filename = serializers.CharField(
label=_('Filename'), label=_('Filename'),
required=False, required=False,
source='get_filename', source='basename',
allow_blank=False,
) )
def update(self, instance, validated_data):
"""
Filename can only be edited on "update"
"""
instance = super().update(instance, validated_data)
print(validated_data)
return instance
class InvenTreeAttachmentSerializerField(serializers.FileField): class InvenTreeAttachmentSerializerField(serializers.FileField):
""" """