title | summary | aliases | |
---|---|---|---|
Connect to TiDB with Django |
Learn how to connect to TiDB using Django. This tutorial gives Python sample code snippets that work with TiDB using Django. |
|
TiDB is a MySQL-compatible database, and Django is a popular web framework for Python, which includes a powerful Object Relational Mapper (ORM) library.
In this tutorial, you can learn how to use TiDB and Django to accomplish the following tasks:
- Set up your environment.
- Connect to your TiDB cluster using Django.
- Build and run your application. Optionally, you can find sample code snippets for basic CRUD operations.
Note:
This tutorial works with TiDB Serverless, TiDB Dedicated, and TiDB Self-Hosted clusters.
To complete this tutorial, you need:
- Python 3.8 or higher.
- Git.
- A TiDB cluster.
If you don't have a TiDB cluster, you can create one as follows:
- (Recommended) Follow Creating a TiDB Serverless cluster to create your own TiDB Cloud cluster.
- Follow Deploy a local test TiDB cluster or Deploy a production TiDB cluster to create a local cluster.
If you don't have a TiDB cluster, you can create one as follows:
- (Recommended) Follow Creating a TiDB Serverless cluster to create your own TiDB Cloud cluster.
- Follow Deploy a local test TiDB cluster or Deploy a production TiDB cluster to create a local cluster.
This section demonstrates how to run the sample application code and connect to TiDB.
Run the following commands in your terminal window to clone the sample code repository:
git clone https://github.com/tidb-samples/tidb-python-django-quickstart.git
cd tidb-python-django-quickstart
Run the following command to install the required packages (including Django, django-tidb, and mysqlclient) for the sample app:
pip install -r requirements.txt
If you encounter installation issues with mysqlclient, refer to the mysqlclient official documentation.
django-tidb
is a TiDB dialect for Django that resolves compatibility issues between TiDB and Django.
To install django-tidb
, choose a version that matches your Django version. For example, if you are using django==4.2.*
, install django-tidb==4.2.*
. The minor version does not need to be the same. It is recommended to use the latest minor version.
For more information, refer to django-tidb repository.
Connect to your TiDB cluster depending on the TiDB deployment option you've selected.
-
Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.
-
Click Connect in the upper-right corner. A connection dialog is displayed.
-
Ensure the configurations in the connection dialog match your operating environment.
- Connection Type is set to
Public
- Branch is set to
main
- Connect With is set to
General
- Operating System matches your environment.
Tip:
If your program is running in Windows Subsystem for Linux (WSL), switch to the corresponding Linux distribution.
- Connection Type is set to
-
Click Generate Password to create a random password.
Tip:
If you have created a password before, you can either use the original password or click Reset Password to generate a new one.
-
Run the following command to copy
.env.example
and rename it to.env
:cp .env.example .env
-
Copy and paste the corresponding connection string into the
.env
file. The example result is as follows:TIDB_HOST='{host}' # e.g. gateway01.ap-northeast-1.prod.aws.tidbcloud.com TIDB_PORT='4000' TIDB_USER='{user}' # e.g. xxxxxx.root TIDB_PASSWORD='{password}' TIDB_DB_NAME='test' CA_PATH='{ssl_ca}' # e.g. /etc/ssl/certs/ca-certificates.crt (Debian / Ubuntu / Arch)
Be sure to replace the placeholders
{}
with the connection parameters obtained from the connection dialog.TiDB Serverless requires a secure connection. Since the
ssl_mode
of mysqlclient defaults toPREFERRED
, you don't need to manually specifyCA_PATH
. Just leave it empty. But if you have a special reason to specifyCA_PATH
manually, you can refer to the TLS connections to TiDB Serverless to get the certificate paths for different operating systems. -
Save the
.env
file.
-
Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.
-
Click Connect in the upper-right corner. A connection dialog is displayed.
-
In the connection dialog, select Public from the Connection Type drop-down list, and then click CA cert to download the CA certificate.
If you have not configured the IP access list, click Configure IP Access List or follow the steps in Configure an IP Access List to configure it before your first connection.
In addition to the Public connection type, TiDB Dedicated supports Private Endpoint and VPC Peering connection types. For more information, see Connect to Your TiDB Dedicated Cluster.
-
Run the following command to copy
.env.example
and rename it to.env
:cp .env.example .env
-
Copy and paste the corresponding connection string into the
.env
file. The example result is as follows:TIDB_HOST='{host}' # e.g. tidb.xxxx.clusters.tidb-cloud.com TIDB_PORT='4000' TIDB_USER='{user}' # e.g. root TIDB_PASSWORD='{password}' TIDB_DB_NAME='test' CA_PATH='{your-downloaded-ca-path}'
Be sure to replace the placeholders
{}
with the connection parameters obtained from the connection dialog, and configureCA_PATH
with the certificate path downloaded in the previous step. -
Save the
.env
file.
-
Run the following command to copy
.env.example
and rename it to.env
:cp .env.example .env
-
Copy and paste the corresponding connection string into the
.env
file. The example result is as follows:TIDB_HOST='{tidb_server_host}' TIDB_PORT='4000' TIDB_USER='root' TIDB_PASSWORD='{password}' TIDB_DB_NAME='test'
Be sure to replace the placeholders
{}
with the connection parameters, and remove theCA_PATH
line. If you are running TiDB locally, the default host address is127.0.0.1
, and the password is empty. -
Save the
.env
file.
In the root directory of the project, run the following command to initialize the database:
python manage.py migrate
-
Run the application in the development mode:
python manage.py runserver
The application runs on port
8000
by default. To use a different port, you can append the port number to the command. The following is an example:python manage.py runserver 8080
-
To access the application, open your browser and go to
http://localhost:8000/
. In the sample application, you can:- Create a new player.
- Bulk create players.
- View all players.
- Update a player.
- Delete a player.
- Trade goods between two players.
You can refer to the following sample code snippets to complete your own application development.
For complete sample code and how to run it, check out the tidb-samples/tidb-python-django-quickstart repository.
In the file sample_project/settings.py
, add the following configurations:
DATABASES = {
"default": {
"ENGINE": "django_tidb",
"HOST": ${tidb_host},
"PORT": ${tidb_port},
"USER": ${tidb_user},
"PASSWORD": ${tidb_password},
"NAME": ${tidb_db_name},
"OPTIONS": {
"charset": "utf8mb4",
},
}
}
TIDB_CA_PATH = ${ca_path}
if TIDB_CA_PATH:
DATABASES["default"]["OPTIONS"]["ssl_mode"] = "VERIFY_IDENTITY"
DATABASES["default"]["OPTIONS"]["ssl"] = {
"ca": TIDB_CA_PATH,
}
You need to replace ${tidb_host}
, ${tidb_port}
, ${tidb_user}
, ${tidb_password}
, ${tidb_db_name}
, and ${ca_path}
with the actual values of your TiDB cluster.
from django.db import models
class Player(models.Model):
name = models.CharField(max_length=32, blank=False, null=False)
coins = models.IntegerField(default=100)
goods = models.IntegerField(default=1)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
For more information, refer to Django models.
# insert a single object
player = Player.objects.create(name="player1", coins=100, goods=1)
# bulk insert multiple objects
Player.objects.bulk_create([
Player(name="player1", coins=100, goods=1),
Player(name="player2", coins=200, goods=2),
Player(name="player3", coins=300, goods=3),
])
For more information, refer to Insert data.
# get a single object
player = Player.objects.get(name="player1")
# get multiple objects
filtered_players = Player.objects.filter(name="player1")
# get all objects
all_players = Player.objects.all()
For more information, refer to Query data.
# update a single object
player = Player.objects.get(name="player1")
player.coins = 200
player.save()
# update multiple objects
Player.objects.filter(coins=100).update(coins=200)
For more information, refer to Update data.
# delete a single object
player = Player.objects.get(name="player1")
player.delete()
# delete multiple objects
Player.objects.filter(coins=100).delete()
For more information, refer to Delete data.
- Learn more usage of Django from the documentation of Django.
- Learn the best practices for TiDB application development with the chapters in the Developer guide, such as Insert data, Update data, Delete data, Single table reading, Transactions, and SQL performance optimization.
- Learn through the professional TiDB developer courses and earn TiDB certifications after passing the exam.
Ask questions on TiDB Community, or create a support ticket.
Ask questions on TiDB Community, or create a support ticket.