Skip to content

Commit

Permalink
Add startup check and support postgres (#10)
Browse files Browse the repository at this point in the history
* Add startup check, support postgres

* Fix the version
  • Loading branch information
viggo-devries authored Jan 19, 2024
1 parent 49f5e96 commit 6d6df16
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions oscar_odin/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ def ready(self):

# Register the Django model field resolver
registration.register_field_resolver(ModelFieldResolver, Model)

# pylint: disable=W0611
from .checks import odin_startup_check
23 changes: 23 additions & 0 deletions oscar_odin/checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django import VERSION
from django.core.checks import Error, register, Tags
from django.conf import settings


# pylint: disable=unused-argument
@register(Tags.compatibility)
def odin_startup_check(app_configs, **kwargs):
errors = []

django_major_version = VERSION[0]

if django_major_version < 4:
for _, database in settings.DATABASES.items():
if database["ENGINE"] == "django.db.backends.sqlite3":
errors.append(
Error(
"django-oscar-odin does not support sqlite3 with Django < 4. Please use engines that have can_return_rows_from_bulk_insert set to True (like Postgres) or upgrade your Django version to 4 or higher.",
id="django-oscar-odin.E001",
)
)

return errors
10 changes: 6 additions & 4 deletions oscar_odin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def in_bulk(self, instances, field_names):
that ID. If `id_list` isn't provided, evaluate the entire QuerySet.
"""

batch_size = (
math.floor(connections[self.db].features.max_query_params / len(field_names))
- 1
)
max_query_params = connections[self.db].features.max_query_params

if max_query_params is not None:
batch_size = math.floor(max_query_params / len(field_names)) - 1
else:
batch_size = None

if batch_size and batch_size < len(instances):
qs = ()
Expand Down

0 comments on commit 6d6df16

Please sign in to comment.