r/FastAPI • u/vherbose • Feb 18 '21
Question ORM or Pydantic model?
Hi, I am building a FastAPI application, but am quite new to this sort of development (making the jump from physics scientific software)--so I have a question about best practices.
I'll lay out an example first and hopefully, my question will be more clear.
from pydantic import BaseModel
from tortoise import fields, models
# ORM model (tortoise)
class User(models.Model):
firstname: fields.CharField(max_length=64)
surname: fields.CharField(max_length=64)
username: fields.CharField(max_length=64)
# Pydantic model (ignore lack of str length constraints)
class User_Pydantic(BaseModel):
firstname: str
surname: str
username: strNow, consider some crude user registration using a POST /register. What is considered best practise? Reading in as a Pydantic model or ORM model? Is it just a matter of choice? I know I can generate Pydantic models from ORM models.
Why I've been thinking about this?
This could be where my inexperience shows, but for my current project, I have defined some tortoise models. The base model is very similar to above. I then have 2 additional models that inherit from the base model. One for storing in the DB--has a hashed password field--and the other is for expected input at /register endpoint--has a plain-text password field (obvs wouldn't want to store this in a DB).
Now, when I run my program, I noticed I have tables in my DB for UserBase, UserInDB and UserIn. I only want the UserInDB table in my DB. So essentially, is there a way to only create the UserInDB table. Or, as I eluded to previously, should I use Pydantic models and only convert to ORM model when interacting with DB (i.e. creating record).
Any questions, please ask. Pretty much never used reddit and I'm not sure why!
8
Feb 18 '21 edited Feb 18 '21
What you call a Pydantic model can also be called a schema.
They use python built-in types. With type hints in the rest of the app, a good practice would be to use the schemas because you will deal with e.g type str instead of CharField.
Basically, you are on the right track.
Maybe you can look at the tiangolo's template project - specifically backend.
You will see two folders relevant to your question called schemas and models.
In the crud part of the project, when interacting with the DB, you create a model from schema, and use that.
2
4
u/iamtheconundrum Feb 19 '21
ORM = interaction with database. Pydantic/schema = validation of requests/payload . They’re two separate things.
1
u/Avico78 Apr 18 '21
Actually theres very nice orm package which combines the pyadnitc and the db module together,it calls ormar and it very well documented with great supportive community, https://github.com/collerek/ormar
0
Feb 19 '21
[deleted]
2
u/vherbose Feb 19 '21
Thanks! I didn’t quite realise this. I knew about pydantic enforcing types, but I’d assumed the ORM model did too
12
u/Clepto2 Feb 19 '21
I use FastAPI heavily in my work and have come to this kind of philosophy regarding Pydantic models vs the ORM models.
Originally I really hated the separation of them and thought it would be much nicer if it could just all be one thing, but what I’ve come to find as I build out more and more and bigger applications is that having them be completely is actually a blessing in disguise.
The genius of the Pydantic models is data validation in my opinion. So you can specify expected types, required/optional fields, etc, and have FastAPI use that validation on the requests. In my work I’ve found it best if inter-application communication is the only place you utilize Pydantic models, and for internal data work, using ORM models.
As an example, let’s say you have a User model. This ORM model likely contains tons of fields that may or may not even be utilized by the API at any point, so you don’t want to build a Pydantic model directly based off of the ORM model. You instead build custom User Pydantic models. This allows you to define a bunch of different types of User data, so maybe you have an Admin API that should return more user data than you get through a public end-user API, you can have separate Pydantic models for each set of data.
In short, I find it’s best to use Pydantic models solely during API communication(validating request data, and what you return from the API), and use ORM models for the APIs internal processing(finding relational data, etc).
This kind of setup is utilized in Tiangolo’s full stack template that was mentioned previously, and I have a ton of experience with the type of setup in that template if you have any questions about it.