Skip to content

Commit 1c60d96

Browse files
committed
Update README.md
1 parent 0838200 commit 1c60d96

File tree

2 files changed

+85
-52
lines changed

2 files changed

+85
-52
lines changed

README.md

Lines changed: 84 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -39,50 +39,44 @@ Use Cloudflare D1, a serverless SQL database, as your Django application's datab
3939
* **Transactions are disabled** for all D1 database engines. Every query is committed immediately.
4040
* The D1 backend has some limitations compared to traditional SQLite or other SQL databases. Many advanced ORM features or direct SQL functions (especially those used in Django Admin) might not be fully supported. Refer to the "Limitations" section.
4141

42-
There are two ways to connect to D1:
43-
44-
#### a) D1 Binding (Recommended for Workers)
45-
46-
When your Django application is running on Cloudflare Workers, use D1 Bindings for the fastest access. The D1 database is made available to your worker via an environment binding.
47-
48-
**Django Settings (`settings.py`):**
49-
```python
50-
DATABASES = {
51-
'default': {
52-
'ENGINE': 'django_cf.d1_binding',
53-
# 'CLOUDFLARE_BINDING' should match the binding name in your wrangler.toml
54-
'CLOUDFLARE_BINDING': 'DB',
42+
**Configuration:**
43+
44+
1. **`wrangler.jsonc` (or `wrangler.toml`):**
45+
```jsonc
46+
{
47+
"d1_databases": [
48+
{
49+
"binding": "DB",
50+
"database_name": "my-django-db",
51+
"database_id": "your-d1-database-id"
52+
}
53+
]
5554
}
56-
}
57-
```
58-
59-
Ensure your `wrangler.toml` includes the D1 database binding:
60-
```toml
61-
[[d1_databases]]
62-
binding = "DB"
63-
database_name = "my-django-db"
64-
database_id = "your-d1-database-id"
65-
```
66-
67-
#### b) D1 API (For Local Development or External Access)
68-
69-
Connect to D1 via its HTTP API. This method is suitable for local development (e.g., running migrations) or when accessing D1 from outside Cloudflare Workers.
70-
71-
**Note:** Accessing D1 via the API can be slower than using bindings due to the nature of HTTP requests for each query.
72-
73-
**Django Settings (`settings.py`):**
74-
```python
75-
DATABASES = {
76-
'default': {
77-
'ENGINE': 'django_cf.d1_api',
78-
'CLOUDFLARE_DATABASE_ID': '<your_database_id>',
79-
'CLOUDFLARE_ACCOUNT_ID': '<your_account_id>',
80-
'CLOUDFLARE_TOKEN': '<your_d1_api_token>', # Ensure this token has D1 Read/Write permissions
55+
```
56+
57+
2. **Worker Entrypoint (`src/index.py`):**
58+
```python
59+
from workers import WorkerEntrypoint
60+
from django_cf import DjangoCF
61+
62+
class Default(DjangoCF, WorkerEntrypoint):
63+
async def get_app(self):
64+
from app.wsgi import application
65+
return application
66+
```
67+
68+
3. **Django Settings (`settings.py`):**
69+
```python
70+
DATABASES = {
71+
'default': {
72+
'ENGINE': 'django_cf.db.backends.d1',
73+
# 'CLOUDFLARE_BINDING' should match the binding name in your wrangler.jsonc
74+
'CLOUDFLARE_BINDING': 'DB',
75+
}
8176
}
82-
}
83-
```
77+
```
8478

85-
For a complete working example, see the [D1 template](templates/d1/).
79+
For a complete working example with full configuration and management endpoints, see the [D1 template](templates/d1/).
8680

8781
### Cloudflare Durable Objects Integration
8882

@@ -92,18 +86,57 @@ Utilize Durable Objects for stateful data persistence directly within your Cloud
9286
* **Transactions are disabled** for Durable Objects. All queries are committed immediately, and rollbacks are not available.
9387
* Durable Objects offer a unique model for state. Understand its consistency and scalability characteristics before implementing.
9488

95-
**Django Settings (`settings.py`):**
96-
```python
97-
DATABASES = {
98-
'default': {
99-
'ENGINE': 'django_cf.do_binding',
89+
**Configuration:**
90+
91+
1. **`wrangler.jsonc` (or `wrangler.toml`):**
92+
Define your Durable Object class and binding.
93+
```jsonc
94+
{
95+
"durable_objects": {
96+
"bindings": [
97+
{
98+
"name": "DO_STORAGE",
99+
"class_name": "DjangoDO"
100+
}
101+
]
102+
},
103+
"migrations": [
104+
{
105+
"tag": "v1",
106+
"new_sqlite_classes": ["DjangoDO"]
107+
}
108+
]
100109
}
101-
}
102-
```
103-
104-
The `django_cf.do_binding` engine will automatically use the Durable Object binding specified in your `wrangler.toml` that is associated with the `DjangoCFDurableObject` class.
110+
```
111+
112+
2. **Worker Entrypoint (`src/index.py`):**
113+
```python
114+
from workers import DurableObject, WorkerEntrypoint
115+
from django_cf import DjangoCFDurableObject
116+
117+
class DjangoDO(DjangoCFDurableObject, DurableObject):
118+
def get_app(self):
119+
from app.wsgi import application
120+
return application
121+
122+
class Default(WorkerEntrypoint):
123+
async def fetch(self, request):
124+
# Route requests to a DO instance
125+
id = self.env.DO_STORAGE.idFromName("singleton_instance")
126+
obj = self.env.DO_STORAGE.get(id)
127+
return await obj.fetch(request)
128+
```
129+
130+
3. **Django Settings (`settings.py`):**
131+
```python
132+
DATABASES = {
133+
'default': {
134+
'ENGINE': 'django_cf.do_binding',
135+
}
136+
}
137+
```
105138

106-
For a complete working example with worker configuration and Durable Object setup, see the [Durable Objects template](templates/durable-objects/).
139+
For a complete working example with full configuration and management endpoints, see the [Durable Objects template](templates/durable-objects/).
107140

108141
## Examples
109142

django_cf/db/backends/d1/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, *args):
3434
self.run_sync = run_sync
3535
except ImportError as e:
3636
print(e)
37-
raise Exception("Code not running inside a worker, please change to django_cf.db.backends.d1_api database backend")
37+
raise Exception("Code not running inside a worker!")
3838

3939

4040
def process_query(self, query, params=None):

0 commit comments

Comments
 (0)