mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 15:15:42 +00:00 
			
		
		
		
	Fix error message styles for API errors
- django ValidationError uses "__all__" key for non_field_errors - whyyyyyyyyyyyy
This commit is contained in:
		@@ -85,8 +85,10 @@ class InvenTreeModelSerializer(serializers.ModelSerializer):
 | 
				
			|||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, instance=None, data=empty, **kwargs):
 | 
					    def __init__(self, instance=None, data=empty, **kwargs):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
        # self.instance = instance
 | 
					        Custom __init__ routine to ensure that *default* values (as specified in the ORM)
 | 
				
			||||||
 | 
					        are used by the DRF serializers, *if* the values are not provided by the user.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # If instance is None, we are creating a new instance
 | 
					        # If instance is None, we are creating a new instance
 | 
				
			||||||
        if instance is None and data is not empty:
 | 
					        if instance is None and data is not empty:
 | 
				
			||||||
@@ -193,7 +195,15 @@ class InvenTreeModelSerializer(serializers.ModelSerializer):
 | 
				
			|||||||
        try:
 | 
					        try:
 | 
				
			||||||
            instance.full_clean()
 | 
					            instance.full_clean()
 | 
				
			||||||
        except (ValidationError, DjangoValidationError) as exc:
 | 
					        except (ValidationError, DjangoValidationError) as exc:
 | 
				
			||||||
            raise ValidationError(detail=serializers.as_serializer_error(exc))
 | 
					
 | 
				
			||||||
 | 
					            data = exc.message_dict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # Change '__all__' key (django style) to 'non_field_errors' (DRF style)
 | 
				
			||||||
 | 
					            if '__all__' in data:
 | 
				
			||||||
 | 
					                data['non_field_errors'] = data['__all__']
 | 
				
			||||||
 | 
					                del data['__all__']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            raise ValidationError(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return data
 | 
					        return data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -173,7 +173,32 @@ function editPart(pk, options={}) {
 | 
				
			|||||||
        title: '{% trans "Edit Part" %}',
 | 
					        title: '{% trans "Edit Part" %}',
 | 
				
			||||||
        reload: true,
 | 
					        reload: true,
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function duplicatePart(pk, options={}) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // First we need all the part information
 | 
				
			||||||
 | 
					    inventreeGet(`/api/part/${pk}/`, {}, {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        success: function(response) {
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            var fields = partFields({
 | 
				
			||||||
 | 
					                duplicate: true
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            constructForm('{% url "api-part-list" %}', {
 | 
				
			||||||
 | 
					                method: 'POST',
 | 
				
			||||||
 | 
					                fields: fields,
 | 
				
			||||||
 | 
					                title: '{% trans "Duplicate Part" %}',
 | 
				
			||||||
 | 
					                data: response,
 | 
				
			||||||
 | 
					                onSuccess: function(data) {
 | 
				
			||||||
 | 
					                    // Follow the new part
 | 
				
			||||||
 | 
					                    location.href = `/part/${data.pk}/`;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user