If you’re tired of rewriting generic CRUD routes or need to rapidly prototype features for presentations or hackathons, you’re in luck! The FastAPI-CRUDRouter makes it incredibly easy. As an extension to the APIRouter included with FastAPI, this tool generates and documents your CRUD routes automatically. Let’s dive into how to set it up and use it effectively.
Installation
To get started, you’ll need to install the FastAPI-CRUDRouter package. Open your terminal and run the following command:
bash
pip install fastapi-crudrouter
Basic Usage
What if I told you that in just ten lines of code, you can generate all the CRUD routes you need for any model? Sounds magical, doesn’t it? Let’s look at a simple example:
python
from pydantic import BaseModel
from fastapi import FastAPI
from fastapi_crudrouter import MemoryCRUDRouter as CRUDRouter
class Potato(BaseModel):
id: int
color: str
mass: float
app = FastAPI()
app.include_router(CRUDRouter(schema=Potato))
Breaking Down the Code: An Analogy
Think of your FastAPI application as a restaurant. The FastAPI
acts as the restaurant itself where customers (users) come to order their meals (data). In our example, the Potato
class represents a menu item, equipped with certain details (id, color, mass) just like how a dish on the menu would have a name, ingredients, and price.
When you include the CRUDRouter in your FastAPI application, it’s like hiring a chef who specializes in preparing the items on your menu. This chef can read the menu (model) and know exactly how to prepare and serve each dish (perform CRUD operations). The chef works efficiently, ensuring your customers get their meals quickly without you having to repeat the same instructions over and over again!
Advanced Usage
The FastAPI-CRUDRouter comes packed with features that maximize your use of automatically generated CRUD routes. Here are some highlights:
- Automatic Pagination – Documentation
- Ability to Provide Custom Create and Update Schemas – Documentation
- Dynamic Generation of Create and Update Schemas – Documentation
- Ability to Add, Customize, or Disable Specific Routes – Documentation
- Native Support for FastAPI Dependency Injection – Documentation
Supported Backends
This handy tool supports several backends (ORMs), with more expected in future updates:
- In Memory – Documentation
- SQLAlchemy – Documentation
- Databases (async) – Documentation
- Gino (async) – Documentation
- Ormar (async) – Documentation
- Tortoise ORM (async) – Documentation
OpenAPI Support
One of the fantastic features of the CRUDRouter is its ability to document all routes generated automatically according to the OpenAPI spec. This provides a snapshot of the routes, just like a well-organized menu in a restaurant!
Troubleshooting Tips
Here are some common issues you might run into and how to resolve them:
- Issue: Routes not generating as expected.
- Double-check your model to ensure it’s defined correctly.
- Ensure that you’ve correctly included the CRUDRouter in your FastAPI application.
- Issue: Error messages during installation.
- Make sure you have the correct environment and Python version set up.
- Try upgrading pip with
pip install --upgrade pip
.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.