Quickstarters
CRUD Samples
How to Build a CRUD App with Python?
34 min
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 https //www back4app com/docs/get started/new parse app 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 https //docs python org/3/ if needed step 1 – setting up your project initiating a new back4app project log into your back4app account select the “new app” button on your dashboard input the project name basic crud app python and follow the prompts to complete the setup 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 you add fields by choosing a data type, naming the field, setting default values, and marking it as required 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 open the ai agent sign in to your back4app dashboard and navigate to the ai agent in your project settings outline your data model provide a comprehensive description detailing the classes and fields needed review and confirm the ai agent will propose a schema based on your input examine the suggestion and confirm to implement it example prompt create the following classes in my back4app project 1\) class items \ fields \ id objectid (auto generated) \ title string \ description string \ createdat date (auto generated) \ updatedat date (auto updated) 2\) class users \ fields \ id objectid (auto generated) \ username string (unique) \ email string (unique) \ passwordhash string \ createdat date (auto generated) \ updatedat date (auto updated) 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 go to the “more” menu on your back4app dashboard select “admin app” and click on “enable admin app ” configure your admin account by setting up initial credentials this will also create roles (like b4aadminuser ) and necessary system classes enable admin app after enabling it, log in to the admin app to manage your app's data 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 pip install requests 2\ example fetching items below is a python script that retrieves items from your back4app project import requests def fetch items() url = "https //parseapi back4app com/classes/items" headers = { "x parse application id" "your application id", "x parse rest api key" "your rest api key" } try response = requests get(url, headers=headers) response raise for status() items = response json() get("results", \[]) print("fetched items ", items) except requests requestexception as error print("error fetching items ", error) if name == " main " fetch items() 3\ creating, updating, and deleting items here are examples for the other crud operations import requests import json base url = "https //parseapi back4app com/classes/items" headers = { "x parse application id" "your application id", "x parse rest api key" "your rest api key", "content type" "application/json" } def create item(title, description) payload = { "title" title, "description" description } try response = requests post(base url, headers=headers, data=json dumps(payload)) response raise for status() print("item created ", response json()) except requests requestexception as error print("creation error ", error) def update item(object id, new title, new description) url = f"{base url}/{object id}" payload = { "title" new title, "description" new description } try response = requests put(url, headers=headers, data=json dumps(payload)) response raise for status() print("item updated ", response json()) except requests requestexception as error print("update error ", error) def delete item(object id) url = f"{base url}/{object id}" try response = requests delete(url, headers=headers) response raise for status() print("item deleted successfully ") except requests requestexception as error print("deletion error ", error) if name == " main " \# example usage create item("sample item", "this is a test item ") \# to update or delete, replace 'object id' with a valid id 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 import requests import json user url = "https //parseapi back4app com/users" user headers = { "x parse application id" "your application id", "x parse rest api key" "your rest api key", "content type" "application/json" } def sign up(username, password, email) payload = { "username" username, "password" password, "email" email } try response = requests post(user url, headers=user headers, data=json dumps(payload)) response raise for status() print("user registered ", response json()) except requests requestexception as error print("registration error ", error) def log in(username, password) url = f"{user url}/login?username={username}\&password={password}" try response = requests get(url, headers=user headers) response raise for status() print("logged in user ", response json()) except requests requestexception as error print("login error ", error) if name == " main " \# example usage sign up("newuser", "securepassword", "user\@example com") log in("newuser", "securepassword") 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 package your application use your preferred method (for instance, creating a virtual environment and packaging your code) 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 basic crud app python/ \| app/ \| | init py \| | main py \| | crud py \| | auth py \| requirements txt \| dockerfile \| readme md 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 \# use an official python runtime as the base image from python 3 9 slim \# set the working directory workdir /app \# install required packages copy requirements txt run pip install no cache dir r requirements txt \# copy application code copy \# expose the port your app will run on (if applicable) expose 5000 \# define the default command to run the app cmd \["python", "app/main py"] 7 4 deploying via back4app web deployment connect your github repository host your python code on github and link it to your back4app account 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 configure build settings set the build command (e g , pip install r requirements txt ) and specify the startup command 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 https //www back4app com/docs and explore more tutorials to refine your application happy coding and enjoy building your python crud application!