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
`TypesenseQuerySet` is required to automatically index model changes on create, update and delete
110
+
How the value of a field is retrieved from a model instance:
111
+
1. The collection field name is called as a property of the model instance
112
+
2. If `value` is provided, it will be called as a property or method of the model instance
113
+
114
+
Where the collections live is totally dependent on you but we recommend having a `collections.py` file in the django app where the model you are creating a collection for is.
109
115
110
-
### Admin Setup
111
-
To update a model admin to display and search from the model Typesense collection, the admin class should inherit from the TypesenseSearchAdminMixin
116
+
> [!NOTE]
117
+
> We recommend displaying data from ForeignKey or OneToOne fields as string attributes using the display decorator to avoid triggering database queries that will negatively affect performance.
118
+
119
+
### Admin Integration
120
+
To make a model admin display and search from the model's Typesense collection, the admin class should inherit `TypesenseSearchAdminMixin`
112
121
113
122
```
114
123
from django_typesense.admin import TypesenseSearchAdminMixin
To update or delete collection documents in bulk. Bulk updating is multi-threaded.
123
-
You might encounter poor performance when indexing large querysets. Suggestions on how to improve are welcome.
137
+
### Indexing
138
+
For the initial setup, you will need to index in bulk. Bulk updating is multi-threaded. Depending on your system specs, you should set the `batch_size` keyword argument.
124
139
125
140
```
126
-
from django_typesense.methods import bulk_delete_typsense_records, bulk_update_typsense_records
127
-
from .models import MyModel
128
-
from django_typesense.typesense_client import client
141
+
from django_typesense.utils import bulk_delete_typsense_records, bulk_update_typsense_records
129
142
130
-
model_qs = Model.objects.all().order_by('date_created') # querysets should be ordered
131
-
bulk_update_typesense_records(model_qs) # for bulk document indexing
132
-
bulk_delete_typsense_records(model_qs) # for bulk document deletiom
143
+
model_qs = Song.objects.all().order_by('id') # querysets should be ordered
To make use of custom admin filters, define a `filter_by` property in the filter definition.
137
-
Define boolean typesense field `has_alien` that gets it's value from a model property.
149
+
Define boolean typesense field `has_views` that gets it's value from a model property. This is example is not necessarily practical but for demo purposes.
138
150
139
151
```
140
-
@property
141
-
def has_alien(self):
142
-
# moon_aliens and mars_aliens are reverse foreign keys
143
-
return self.moon_aliens.exists() or self.mars_aliens.exists()
152
+
# models.py
153
+
class Song(models.Model):
154
+
...
155
+
@property
156
+
def has_views(self):
157
+
return self.number_of_views > 0
158
+
...
159
+
160
+
# collections.py
161
+
class SongCollection(TypesenseCollection):
162
+
...
163
+
has_views = fields.TypesenseBooleanField()
164
+
...
144
165
```
145
166
146
167
```
147
-
class HasAlienFilter(admin.SimpleListFilter):
148
-
title = _('Has Alien')
149
-
parameter_name = 'has_alien'
168
+
class HasViewsFilter(admin.SimpleListFilter):
169
+
title = _('Has Views')
170
+
parameter_name = 'has_views'
150
171
151
172
def lookups(self, request, model_admin):
152
173
return (
@@ -158,54 +179,20 @@ class HasAlienFilter(admin.SimpleListFilter):
0 commit comments