Skip to content

Conversation

@oneashkan
Copy link

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.

### 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.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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant