VOOZH about

URL: https://www.geeksforgeeks.org/python/python-falcon-sqlalchemy-models/

⇱ Python Falcon - SQLAlchemy Models - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Falcon - SQLAlchemy Models

Last Updated : 23 Jul, 2025

Python Falcon is an up-to-date web framework with an adjustable applicative architecture that is oriented to creating high-speed application processes and APIs. Another often implemented pattern is CRUD and interaction with the database can be facilitated with the help of SQLAlchemy for Python. When used with SQLAlchemy, Falcon provides developers with the tools to build great web applications using relations databases as backends that are optimized, easy to maintain, and scalable.

Setting Up the Environment

First of all, it is necessary to install both Falcon and SQLAlchemy as they will be the core libraries used in the following model. You can do this using pip:

pip install falcon sqlalchemy

Creating a Falcon Application with SQLAlchemy

If you are implementing the API, the next step after completing the creation of the Falcon application is creating a Falcon application with SQLAlchemy.

To further demonstrate how Falcon works withe h SQLAlchemy, let’s create a rather stripped-down API for managing users. It will be designed to perform four operations namely, create, read, update, and delete with respect to users.

Step 1: Introduction to SQLAlchemy and define the SQLAlchemy Models

Firstly, it is necessary to define our models based on the SQLAlchemy toolkit. The final step is to create a file with the name β€˜models. py':

This code defines a User model with three fields: id, name, and email. It also sets up an SQLite database and a session maker for interacting with the database.

Step 2: Creating Falcon Resources

Next, create a file named resources.py where we define our Falcon resources. These resources will manage the HTTP requests and also the communication with the SQLAlchemy models.

This particular resource class contains methods for handling the HTTP methods GET, POST, PUT and DELETE. Both methods use SQLAlchemy sessions to query the database for CRUD operations.

Step 3: Setting Up the Falcon Application

Now, create a file named app.py to set up the Falcon application and add routes for the user resource.

Step 4: Running the Application

Create a file named server.py to run the Falcon application:

Start the server:

python server.py

Step 5: Testing the Endpoints using CURL

Use a tool like Postman or cURL to test the endpoints.

1. Create a User (POST /users)

curl -X POST http://127.0.0.1:8000/users -d '{"name": "John Doe", "email": "john@example.com"}' -H 'Content-Type: application/json'
πŸ‘ g
Create

2. Get a User (GET /users/1)

curl http://127.0.0.1:8000/users/1
πŸ‘ r
Read

3. Update a User (PUT /users/1)

curl -X PUT http://127.0.0.1:8000/users/1 -d '{"name": "Jane Doe", "email": "jane@example.com"}' -H 'Content-Type: application/json'
πŸ‘ u
Update

4. Delete a User (DELETE /users/1)

curl -X DELETE http://127.0.0.1:8000/users/1
πŸ‘ d
Delete

Testing the Endpoints Using Postman

1. Create a User (POST /users)

  • Open Postman.
  • Select POST from the dropdown menu.
  • Enter the URL: http://127.0.0.1:8000/users.
  • Go to the Body tab and select raw. Choose JSON from the dropdown menu.
  • Enter the following JSON data:
{
"name": "Jane Doe",
"email": "jane@example.com"
}
  • Click Send.
  • You should see a response with the user's details and a status code 201 Created.

2. Get a User (GET /users/1)

  • Select GET from the dropdown menu.
  • Enter the URL:
http://127.0.0.1:8000/users/1.
  • Click Send.
  • You should see a response with the user's details if the user with ID 1 exists.

3. Update a User (PUT /users/1)

  • Select PUT from the dropdown menu.
  • Enter the URL:
http://127.0.0.1:8000/users/1.
  • Go to the Body tab and select raw. Choose JSON from the dropdown menu.
  • Enter the following JSON data:
{
"name": "Jane Doe",
"email": "jane@google.com"
}
  • Click Send.
  • You should see a response with the updated user's details.

4. Delete a User (DELETE /users/1)

  • Select DELETE from the dropdown menu.
  • Enter the URL:
http://127.0.0.1:8000/users/1.
  • Click Send.
  • You should see a response with status code 204 No Content if the user was successfully deleted.

Conclusion

To develop databases web applications, SQLAlchemy coupled with Falcon can provide you with a reliable and robust solution. This concatenation results to neat and manageable code and it is a good platform to build future expandable APIs. With this article, you can create an app using Falcon as the basis for performing CRUD operations using SQLAlchemy models.

Comment
Article Tags: