Django-Styleguide-Example
Django-Styleguide-Example copied to clipboard
Update services.py
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/exceptblock aroundinstance.full_clean()andinstance.save(). - Converted
django.core.exceptions.ValidationError→rest_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.