Django-Styleguide-Example icon indicating copy to clipboard operation
Django-Styleguide-Example copied to clipboard

Update services.py

Open oneashkan opened this issue 3 months ago • 0 comments

Summary

This pull request improves the model_update function by adding proper handling for Django ValidationError. It now converts it into DRF's ValidationError to ensure consistent API responses (HTTP 400 instead of 500).

Changes

  • Added try/except block around instance.full_clean() and instance.save().
  • Converted django.core.exceptions.ValidationErrorrest_framework.exceptions.ValidationError.
  • Ensured clean, consistent error handling across all model update operations.

Why

Previously, unhandled validation errors resulted in server errors (500). This update ensures that all validation issues are returned properly as JSON API responses.

Testing

  • Manually tested with invalid model fields.
  • Confirmed correct 400 responses in API.

oneashkan avatar Oct 16 '25 13:10 oneashkan