You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+84-51Lines changed: 84 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,50 +39,44 @@ Use Cloudflare D1, a serverless SQL database, as your Django application's datab
39
39
***Transactions are disabled** for all D1 database engines. Every query is committed immediately.
40
40
* 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.
41
41
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
+
]
55
54
}
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.**WorkerEntrypoint (`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
+
}
81
76
}
82
-
}
83
-
```
77
+
```
84
78
85
-
For a complete working example, see the [D1 template](templates/d1/).
79
+
For a complete working examplewith full configuration and management endpoints, see the [D1 template](templates/d1/).
86
80
87
81
### Cloudflare Durable Objects Integration
88
82
@@ -92,18 +86,57 @@ Utilize Durable Objects for stateful data persistence directly within your Cloud
92
86
***Transactions are disabled**for Durable Objects. All queries are committed immediately, and rollbacks are not available.
93
87
* Durable Objects offer a unique model forstate. Understand its consistency and scalability characteristics before implementing.
94
88
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 Objectclassand 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
+
]
100
109
}
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.**WorkerEntrypoint (`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
+
```
105
138
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 withfull configuration and management endpoints, see the [Durable Objects template](templates/durable-objects/).
0 commit comments