Skip to content

Commit 5823508

Browse files
committed
black
1 parent 2383971 commit 5823508

10 files changed

+148
-118
lines changed

manage.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def main():
88
"""Run administrative tasks."""
9-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pgml_todo.settings')
9+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pgml_todo.settings")
1010
try:
1111
from django.core.management import execute_from_command_line
1212
except ImportError as exc:
@@ -18,5 +18,5 @@ def main():
1818
execute_from_command_line(sys.argv)
1919

2020

21-
if __name__ == '__main__':
21+
if __name__ == "__main__":
2222
main()

pgml_todo/asgi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
from django.core.asgi import get_asgi_application
1313

14-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pgml_todo.settings')
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pgml_todo.settings")
1515

1616
application = get_asgi_application()

pgml_todo/settings.py

+36-36
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
2323

2424
# SECURITY WARNING: keep the secret key used in production secret!
25-
SECRET_KEY = 'django-insecure-&k&_wb5#iu4c9kwfrm^o7d6xwk965nsdmxo%d!k94fagg^ht1#'
25+
SECRET_KEY = "django-insecure-&k&_wb5#iu4c9kwfrm^o7d6xwk965nsdmxo%d!k94fagg^ht1#"
2626

2727
# SECURITY WARNING: don't run with debug turned on in production!
2828
DEBUG = True
@@ -33,52 +33,52 @@
3333
# Application definition
3434

3535
INSTALLED_APPS = [
36-
'django.contrib.admin',
37-
'django.contrib.auth',
38-
'django.contrib.contenttypes',
39-
'django.contrib.sessions',
40-
'django.contrib.messages',
41-
'django.contrib.staticfiles',
42-
'todo',
43-
'rest_framework',
36+
"django.contrib.admin",
37+
"django.contrib.auth",
38+
"django.contrib.contenttypes",
39+
"django.contrib.sessions",
40+
"django.contrib.messages",
41+
"django.contrib.staticfiles",
42+
"todo",
43+
"rest_framework",
4444
]
4545

4646
MIDDLEWARE = [
47-
'django.middleware.security.SecurityMiddleware',
48-
'django.contrib.sessions.middleware.SessionMiddleware',
49-
'django.middleware.common.CommonMiddleware',
50-
'django.middleware.csrf.CsrfViewMiddleware',
51-
'django.contrib.auth.middleware.AuthenticationMiddleware',
52-
'django.contrib.messages.middleware.MessageMiddleware',
53-
'django.middleware.clickjacking.XFrameOptionsMiddleware',
47+
"django.middleware.security.SecurityMiddleware",
48+
"django.contrib.sessions.middleware.SessionMiddleware",
49+
"django.middleware.common.CommonMiddleware",
50+
"django.middleware.csrf.CsrfViewMiddleware",
51+
"django.contrib.auth.middleware.AuthenticationMiddleware",
52+
"django.contrib.messages.middleware.MessageMiddleware",
53+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
5454
]
5555

56-
ROOT_URLCONF = 'pgml_todo.urls'
56+
ROOT_URLCONF = "pgml_todo.urls"
5757

5858
TEMPLATES = [
5959
{
60-
'BACKEND': 'django.template.backends.django.DjangoTemplates',
61-
'DIRS': [],
62-
'APP_DIRS': True,
63-
'OPTIONS': {
64-
'context_processors': [
65-
'django.template.context_processors.debug',
66-
'django.template.context_processors.request',
67-
'django.contrib.auth.context_processors.auth',
68-
'django.contrib.messages.context_processors.messages',
60+
"BACKEND": "django.template.backends.django.DjangoTemplates",
61+
"DIRS": [],
62+
"APP_DIRS": True,
63+
"OPTIONS": {
64+
"context_processors": [
65+
"django.template.context_processors.debug",
66+
"django.template.context_processors.request",
67+
"django.contrib.auth.context_processors.auth",
68+
"django.contrib.messages.context_processors.messages",
6969
],
7070
},
7171
},
7272
]
7373

74-
WSGI_APPLICATION = 'pgml_todo.wsgi.application'
74+
WSGI_APPLICATION = "pgml_todo.wsgi.application"
7575

7676

7777
# Database
7878
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
7979

8080
DATABASES = {
81-
'default': dj_database_url.parse(os.environ.get('DATABASE_URL')),
81+
"default": dj_database_url.parse(os.environ.get("DATABASE_URL")),
8282
}
8383

8484

@@ -87,26 +87,26 @@
8787

8888
AUTH_PASSWORD_VALIDATORS = [
8989
{
90-
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
90+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
9191
},
9292
{
93-
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
93+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
9494
},
9595
{
96-
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
96+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
9797
},
9898
{
99-
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
99+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
100100
},
101101
]
102102

103103

104104
# Internationalization
105105
# https://docs.djangoproject.com/en/5.0/topics/i18n/
106106

107-
LANGUAGE_CODE = 'en-us'
107+
LANGUAGE_CODE = "en-us"
108108

109-
TIME_ZONE = 'UTC'
109+
TIME_ZONE = "UTC"
110110

111111
USE_I18N = True
112112

@@ -116,9 +116,9 @@
116116
# Static files (CSS, JavaScript, Images)
117117
# https://docs.djangoproject.com/en/5.0/howto/static-files/
118118

119-
STATIC_URL = 'static/'
119+
STATIC_URL = "static/"
120120

121121
# Default primary key field type
122122
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
123123

124-
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
124+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

pgml_todo/urls.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
1. Import the include() function: from django.urls import include, path
1515
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1616
"""
17+
1718
from django.contrib import admin
1819
from django.urls import path, include
1920
from todo.views import TodoItemViewSet
2021

2122
from rest_framework import routers
2223

2324
router = routers.DefaultRouter()
24-
router.register(r'todo', TodoItemViewSet)
25+
router.register(r"todo", TodoItemViewSet)
2526

2627
urlpatterns = [
27-
path('admin/', admin.site.urls),
28-
path('api/', include(router.urls)),
28+
path("admin/", admin.site.urls),
29+
path("api/", include(router.urls)),
2930
]

pgml_todo/wsgi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
from django.core.wsgi import get_wsgi_application
1313

14-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pgml_todo.settings')
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pgml_todo.settings")
1515

1616
application = get_wsgi_application()

todo/apps.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33

44
class TodoConfig(AppConfig):
5-
default_auto_field = 'django.db.models.BigAutoField'
6-
name = 'todo'
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "todo"

todo/migrations/0001_initial.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,32 @@ class Migration(migrations.Migration):
99

1010
initial = True
1111

12-
dependencies = [
13-
]
12+
dependencies = []
1413

1514
operations = [
1615
migrations.CreateModel(
17-
name='TodoItem',
16+
name="TodoItem",
1817
fields=[
19-
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20-
('description', models.CharField(max_length=256)),
21-
('due_date', models.DateField()),
22-
('completed', models.BooleanField(default=False)),
23-
('embedding', models.GeneratedField(db_persist=True, expression=todo.models.EmbedSmallExpression('description'), output_field=pgvector.django.VectorField(dimensions=384))),
18+
(
19+
"id",
20+
models.BigAutoField(
21+
auto_created=True,
22+
primary_key=True,
23+
serialize=False,
24+
verbose_name="ID",
25+
),
26+
),
27+
("description", models.CharField(max_length=256)),
28+
("due_date", models.DateField()),
29+
("completed", models.BooleanField(default=False)),
30+
(
31+
"embedding",
32+
models.GeneratedField(
33+
db_persist=True,
34+
expression=todo.models.EmbedSmallExpression("description"),
35+
output_field=pgvector.django.VectorField(dimensions=384),
36+
),
37+
),
2438
],
2539
),
2640
]

todo/migrations/0002_todoitem_todoitem_embedding_hnsw_index.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
class Migration(migrations.Migration):
88

99
dependencies = [
10-
('todo', '0001_initial'),
10+
("todo", "0001_initial"),
1111
]
1212

1313
operations = [
1414
migrations.AddIndex(
15-
model_name='todoitem',
16-
index=pgvector.django.HnswIndex(ef_construction=64, fields=['embedding'], m=16, name='todoitem_embedding_hnsw_index', opclasses=['vector_cosine_ops']),
15+
model_name="todoitem",
16+
index=pgvector.django.HnswIndex(
17+
ef_construction=64,
18+
fields=["embedding"],
19+
m=16,
20+
name="todoitem_embedding_hnsw_index",
21+
opclasses=["vector_cosine_ops"],
22+
),
1723
),
1824
]

todo/models.py

+46-42
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,61 @@
33

44
from rest_framework import serializers
55

6+
67
class EmbedSmallExpression(models.Expression):
7-
"""
8-
An expression to automatically embed any text column in the same table.
9-
"""
10-
output_field = VectorField(null=False, blank=True, dimensions=384)
8+
"""
9+
An expression to automatically embed any text column in the same table.
10+
"""
11+
12+
output_field = VectorField(null=False, blank=True, dimensions=384)
1113

12-
def __init__(self, field):
13-
self.embedding_field = field
14+
def __init__(self, field):
15+
self.embedding_field = field
1416

15-
def as_sql(self, compiler, connection, template=None):
16-
return f"pgml.embed('intfloat/e5-small', {self.embedding_field})", None
17+
def as_sql(self, compiler, connection, template=None):
18+
return f"pgml.embed('intfloat/e5-small', {self.embedding_field})", None
1719

1820

1921
class TodoItem(models.Model):
20-
"""
21-
TODO item.
22-
"""
23-
description = models.CharField(max_length=256)
24-
due_date = models.DateField()
25-
completed = models.BooleanField(default=False)
26-
embedding = models.GeneratedField(
27-
expression=EmbedSmallExpression("description"),
28-
output_field=VectorField(null=False, blank=False, dimensions=384),
29-
db_persist=True,
30-
)
31-
32-
def __str__(self):
33-
return self.description
34-
35-
class Meta:
36-
indexes = [
37-
# Adding an HNSW index on the embedding column
38-
# allows approximate nearest neighbor searches on datasets
39-
# that are so large that the exact search would be too slow.
40-
#
41-
# The trade-off here is the results are approximate and
42-
# may miss some of the nearest neighbors.
43-
HnswIndex(
44-
name='todoitem_embedding_hnsw_index',
45-
fields=['embedding'],
22+
"""
23+
TODO item.
24+
"""
25+
26+
description = models.CharField(max_length=256)
27+
due_date = models.DateField()
28+
completed = models.BooleanField(default=False)
29+
embedding = models.GeneratedField(
30+
expression=EmbedSmallExpression("description"),
31+
output_field=VectorField(null=False, blank=False, dimensions=384),
32+
db_persist=True,
33+
)
34+
35+
def __str__(self):
36+
return self.description
37+
38+
class Meta:
39+
indexes = [
40+
# Adding an HNSW index on the embedding column
41+
# allows approximate nearest neighbor searches on datasets
42+
# that are so large that the exact search would be too slow.
43+
#
44+
# The trade-off here is the results are approximate and
45+
# may miss some of the nearest neighbors.
46+
HnswIndex(
47+
name="todoitem_embedding_hnsw_index",
48+
fields=["embedding"],
4649
m=16,
4750
ef_construction=64,
48-
opclasses=['vector_cosine_ops']
51+
opclasses=["vector_cosine_ops"],
4952
)
50-
]
53+
]
5154

5255

5356
class TodoItemSerializer(serializers.ModelSerializer):
54-
"""
55-
TODO item serializer used by the Django Rest Framework.
56-
"""
57-
class Meta:
58-
model = TodoItem
59-
fields = ['id', 'description', 'due_date', 'completed', 'embedding']
57+
"""
58+
TODO item serializer used by the Django Rest Framework.
59+
"""
60+
61+
class Meta:
62+
model = TodoItem
63+
fields = ["id", "description", "due_date", "completed", "embedding"]

0 commit comments

Comments
 (0)