Quickstarters

How to Build a CRUD App with Python?

34min

Overview

In this tutorial, you'll learn to construct a basic CRUD (create, read, update, delete) application using Python.

We will harness Back4app as the backend platform to simplify your data management. This guide walks you through setting up a Back4app project, creating a flexible data schema, and coding Python scripts to execute CRUD operations via REST API calls.

Initially, you will establish a Back4app project called Basic-CRUD-App-Python that provides a scalable, non-relational data storage solution.

You will outline your data model by defining classes and their fields, either manually through the Back4app dashboard or with the assistance of the integrated AI Agent.

Next, you will explore the Back4app Admin App, a drag-and-drop interface that simplifies managing your data.

Finally, you'll link your Python application to Back4app by making RESTful API calls to perform secure CRUD operations.

After completing this guide, you'll have developed a production-ready Python application that performs core CRUD tasks along with secure user authentication and data management.

What You Will Learn

  • How to create a Python-based CRUD app with a robust, non-relational backend.
  • Techniques for building and integrating a scalable backend with your Python code.
  • How to efficiently use the Back4app Admin App for managing data records.
  • Deployment approaches, including containerization with Docker for your Python application.


Prerequisites

Ensure you have the following before proceeding:

  • A Back4app account with a newly configured project. Need help? Visit Getting Started with Back4app.
  • A Python development setup. Use your preferred IDE (such as PyCharm or VS Code) and ensure Python 3.7+ is installed.
  • Basic understanding of Python, object-oriented programming, and REST APIs. Refer to the Python documentation if needed.


Step 1 – Setting Up Your Project

Initiating a New Back4app Project

  1. Log into your Back4app account.
  2. Select the “New App” button on your dashboard.
  3. Input the project name: Basic-CRUD-App-Python and follow the prompts to complete the setup.
Create New Project
Create New Project


Once set up, your project appears on the dashboard, laying the foundation for your backend configuration.



Step 2 – Crafting Your Data Model

Defining Your Data Structures

For this CRUD application, you need to define multiple classes (collections) in your Back4app project. The examples below outline the essential classes and their corresponding fields needed for basic CRUD operations.

1. Items Class

This class stores information about each item.

Field

Data Type

Description

_id

ObjectId

Automatically generated unique identifier.

title

String

Name of the item.

description

String

Brief overview of the item.

createdAt

Date

Timestamp marking when the item was created.

updatedAt

Date

Timestamp marking the last modification.

2. Users Class

This class handles user credentials and authentication.

Field

Data Type

Description

_id

ObjectId

Auto-generated unique identifier.

username

String

Unique username for the user.

email

String

User's unique email address.

passwordHash

String

Hashed password for secure authentication.

createdAt

Date

Timestamp when the account was created.

updatedAt

Date

Timestamp when the account was last updated.

You can manually create these classes and specify fields through the Back4app dashboard.

Create New Class
Create New Class


You add fields by choosing a data type, naming the field, setting default values, and marking it as required.

Create Column
Create Column


Using the Back4app AI Agent for Schema Configuration

The Back4app AI Agent is a smart tool embedded in your dashboard that can auto-generate your data schema based on your requirements. This feature accelerates project setup and guarantees that your model supports all necessary CRUD functions.

How to Leverage the AI Agent:

  1. Open the AI Agent: Sign in to your Back4app dashboard and navigate to the AI Agent in your project settings.
  2. Outline Your Data Model: Provide a comprehensive description detailing the classes and fields needed.
  3. Review and Confirm: The AI Agent will propose a schema based on your input. Examine the suggestion and confirm to implement it.

Example Prompt

Text


This approach saves time and ensures that your data model is well-optimized for your application needs.



Step 3 – Enabling the Admin App & Performing CRUD Operations

An Introduction to the Admin App

The Back4app Admin App is a no-code interface that allows you to manage your backend data efficiently. Its intuitive drag-and-drop features let you create, view, update, and delete records with ease.

Activating the Admin App

  1. Go to the “More” menu on your Back4app dashboard.
  2. Select “Admin App” and click on “Enable Admin App.”
  3. Configure your admin account by setting up initial credentials. This will also create roles (like B4aAdminUser) and necessary system classes.
Enable Admin App
Enable Admin App


After enabling it, log in to the Admin App to manage your app's data.

Admin App Dashboard
Admin App Dashboard


Using the Admin App for CRUD Operations

Within the Admin App, you can:

  • Insert Records: Choose “Add Record” in a class (e.g., Items) to add new data.
  • View and Edit Records: Click on an entry to see its details or update fields.
  • Remove Records: Delete records that are no longer needed.

This user-friendly interface makes managing data a breeze.



Step 4 – Connecting Your Python App to Back4app

With your backend ready, the next phase is linking your Python application to Back4app.

Utilizing REST API Calls in Python

Since an official Parse SDK for Python is not available, you will interact with Back4app using REST API calls. Python's requests library is perfect for this.

1. Installing Required Library

Run the following command to install the requests package:

Bash


2. Example: Fetching Items

Below is a Python script that retrieves items from your Back4app project:

Python


3. Creating, Updating, and Deleting Items

Here are examples for the other CRUD operations:

Python


Integrate these functions into your application logic as needed.



Step 5 – Enhancing Backend Security

Implementing Access Control

Protect your data by setting up access control rules. For instance, you can ensure that only the owner of an item can view or modify it by using specific ACL settings through your API calls.

When creating a private item, include ACL settings in your payload. Detailed configuration can be handled in your Back4app dashboard.

Setting Class-Level Permissions (CLPs)

Adjust CLPs in your Back4app project settings to enforce default security policies, ensuring that only authenticated users have access to certain classes.



Step 6 – Implementing User Authentication

Managing User Registration and Login

Back4app supports user authentication through its built-in User class. The following examples demonstrate how you can register and authenticate users using Python REST API calls.

Python


This setup supports session management, password resets, and other authentication features.



Step 7 – Deploying Your Python Application

Back4app provides a straightforward deployment process. You can deploy your Python application using methods such as Docker containerization.

7.1 Building Your Python Application

  1. Package your Application: Use your preferred method (for instance, creating a virtual environment and packaging your code).
  2. Test the Package: Ensure that all dependencies are installed and your scripts are working as expected.


7.2 Organizing Your Project Structure

A typical Python project may look like:

Text


For example, your crud.py might include the functions shown above for handling items.

7.3 Dockerizing Your Python Application

To containerize your app, include a Dockerfile in your project directory:

Dockerfile


7.4 Deploying via Back4app Web Deployment

  1. Connect Your GitHub Repository: Host your Python code on GitHub and link it to your Back4app account.
  2. Set Deployment Options: In the Back4app dashboard, navigate to the Web Deployment section, select your repository (e.g., Basic-CRUD-App-Python), and choose the branch.
  3. Configure Build Settings: Set the build command (e.g., pip install -r requirements.txt) and specify the startup command.
  4. Deploy Your App: Click Deploy and monitor the logs until your application is live.


Step 8 – Wrapping Up and Future Directions

Great job! You have successfully built a Python-based CRUD application integrated with Back4app.

You set up a project named Basic-CRUD-App-Python, defined classes for Items and Users, and managed your data via the Back4app Admin App.

Additionally, you connected your Python scripts to Back4app using REST API calls and implemented solid security measures.

Next Steps:

  • Expand Your Application: Introduce additional features like advanced filtering, detailed views, or live updates.
  • Enhance Backend Capabilities: Consider integrating cloud functions, external APIs, or advanced role-based access controls.
  • Deepen Your Skills: Visit the Back4app documentation and explore more tutorials to refine your application.

Happy coding and enjoy building your Python CRUD application!