I am looking to implement Pydantic into my Django app (for learning and interview purposes), but I'm wondering why would I? Why should I use data validation on things like Django models when the fields themselves already have types declared on them? Are there any other instances in a Django app where it makes sense to use data validation? Please be kind in your responses. I am genuinely curious and asking honestly.
I have tried researching reasons to use Pydantic in a Django app, but have not found any strong arguments for its use.
Pydantic does not have any overlapping use case with Django Models. It has overlapping use case with Django Forms.
Django models allows you to do database/model level validation, but models does not do any parsing from serialised data and it doesn't produce error messages in a form that you can use for customers.
Also many of model validations are done through database validations, and checking for database validations requires that you send the query to the database, trigger some constraint errors, and then having to revert the database cursor state through a savepoint/atomic block. That recovery strategy is expensive in the database when the database is the hardest component to scale. And also, the database generally don't really produce any error messages that you can pass on to the user; their error messages usually refer to the internal db field names and the error may just refer to the name of the constraints being validated.
Instead, the common strategy in Django applications is usually to pre validate as much as possible in the application layer, using Django Forms, before sending a query that is mostly should already be valid to the database. That reduces the type of errors the database can produce to things like unique constraint errors that can't really be checked at the application level.
Pydantic doesn't have overlapping functionality with Django Models. Its functionality overlaps more with Django Forms.
Why might you want to use Pydantic in a Django application? Django Form is heavily oriented towards the use case of working with HTML Forms. If your application is serving an API, Pydantic can be less awkward than Django Forms.
Pydantic does parsing, type coercion, validations, and serialisation for arbitrarily nested JSON and dict like objects. Forms does all that too, but only for flat dictionary.
However, a more common option for working with APIs in Django would be Django REST Framework serializers. DRF is much better integrated with the rest of Django.
In practice, if you're using Pydantic in a Django project, it's much more likely you're not using it to replace forms or models, but rather you use it when you need to validate data in other places, like when making JSON API requests to other APIs or non-ORM database, or to parse local files.
To answer some of the sub-questions in your question:
You wouldn't. That's not what Pydantic is really used for in web apps.
Absolutely: Forms and API requests require validation, which you can use Pydantic for, but thats also not what Pydantic is really used for in web apps.
It appears you're (not unreasonably) confused by the term validation, which Pydantic themselves (copied from https://docs.pydantic.dev/latest/concepts/models/) admit is confusing:
We use the term "validation" to refer to the process of instantiating a model (or other type) that adheres to specified types and constraints. This task, which Pydantic is well known for, is most widely recognized as "validation" in colloquial terms, even though in other contexts the term "validation" may be more restrictive.
Essentially Pydantic is used to create model object with guaranteed structures and types, from other objects. While this absolutely can be used for validation, it can also be used for serialization. So translating your ORM models into JSON for GET requests, much like DRF as well as automatically returning a 422 if PUT or POST data (json) doesn't match a certain schema, and making it easier to work with in your endpoint. That's what 99% of Pydantic use in web apps is about.
This question really becomes "why would I use Pydantic instead of DRF" and the reasons for that include:
At least one effort has been made to make DRF work with Pydantic: drf-pydantic however a more rounded project is django-ninja which uses a Pydantic based alternative to DRF, inspired by FastAPI whose homepagepage some very basic benchmark illustrations showing the kind of performance difference you could see.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With