-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Looks like model_validator is not called during drf validation #25
Comments
This is expected - you are validating payload using DRF serializer, not using the pydantic model. After model class declaration (not instantiation), DRF serializer and pydanitc model become fully separate objects. The only things that carry over between the two are fields and field constraints. Field validators, model validators, properties, or any other custom code you write for your pydantic models cannot be ported to DRF serializer. |
I appreciate the clarification. I understand the design choice better now. However, this behavior does pose a challenge for those of us who rely on Pydantic's field validators for comprehensive validation. Without these validators being executed during the DRF Is it possible to integrate Pydantic's validation logic within the DRF serializer process in future versions? This would greatly enhance the utility of your library by fully leveraging the strengths of both Pydantic and DRF. Thank you for considering this feedback. |
Thank you for your suggestion. I understand what you are asking for and perhaps this could be implemented, but with making a custom subclass of a DRF serializer to couple the models together. We need to agree on the expected behavior: what should happen when you call |
Thank you for considering this enhancement. The most logical behavior would be to mimic Pydantic's validation process within the DRF serializer. Specifically, when is_valid is called on the DRF serializer, it should also validate the data against the Pydantic model. Whether to continue using DRF's internal validation (by mapping Pydantic fields to DRF fields) or to rely solely on Pydantic's validation (acting as a wrapper for Pydantic fields) remains an open question. Either way, any validation errors should be raised in a way that conforms to DRF standards, including appropriate HTTP response codes and error response bodies structured as dictionaries with fields and errors. |
Repro:
The text was updated successfully, but these errors were encountered: