8000 fix: Support for autofields in non-default databases · googleapis/python-spanner-django@e02e390 · GitHub
[go: up one dir, main page]

Skip to content

Commit e02e390

Browse files
committed
fix: Support for autofields in non-default databases
Fix #783 Also refactored code a bit such that version detection is no longer used to determine whether BigAutoField exists. BigAutoField exists in Django versions older than 3.0, and the way this is currently monkeypatched prevents us from using any BigAutoField in Spanner with Django 2.2.
1 parent 4db7ce0 commit e02e390

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

django_spanner/__init__.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@
1414

1515
import pkg_resources
1616
from google.cloud.spanner_v1 import JsonObject
17-
from django.db.models.fields import (
18-
NOT_PROVIDED,
19-
AutoField,
20-
Field,
21-
)
17+
from django.db.models import fields
2218

2319
from .expressions import register_expressions
2420
from .functions import register_functions
< 10000 div class="d-flex flex-row">
@@ -35,10 +31,6 @@
3531
USING_DJANGO_3 = True
3632

3733
if USING_DJANGO_3:
38-
from django.db.models.fields import (
39-
SmallAutoField,
40-
BigAutoField,
41-
)
4234
from django.db.models import JSONField
4335

4436
__version__ = pkg_resources.get_distribution("django-google-spanner").version
@@ -58,29 +50,29 @@ def gen_rand_int64():
5850
# Credit to https://stackoverflow.com/a/3530326.
5951
return uuid4().int & 0x7FFFFFFFFFFFFFFF
6052

53+
def _fix_id_generator(cls):
54+
old_get_db_prep_value = cls.get_db_prep_value
6155

62-
def autofield_init(self, *args, **kwargs):
63-
kwargs["blank"] = True
64-
Field.__init__(self, *args, **kwargs)
56+
def spanner_autofield_get_db_prep_value(self, value, connection, prepared=False):
57+
value = old_get_db_prep_value(self, value, connection, prepared)
6558

66-
if (
67-
django.db.connection.settings_dict["ENGINE"] == "django_spanner"
68-
and self.default == NOT_PROVIDED
69-
):
70-
self.default = gen_rand_int64
59+
if (
60+
connection.settings_dict["ENGINE"] == "django_spanner"
61+
and value is None
62+
):
63+
value = gen_rand_int64()
7164

65+
return value
7266

73-
AutoField.__init__ = autofield_init
74-
AutoField.db_returning = False
75-
AutoField.validators = []
76-
if USING_DJANGO_3:
77-
SmallAutoField.__init__ = autofield_init
78-
BigAutoField.__init__ = autofield_init
79-
SmallAutoField.db_returning = False
80- 8965
BigAutoField.db_returning = False
81-
SmallAutoField.validators = []
82-
BigAutoField.validators = []
67+
cls.get_db_prep_value = spanner_autofield_get_db_prep_value
68+
cls.db_returning = False
69+
cls.validators = []
8370

71+
for field_cls_name in ("AutoField", "BigAutoField", "SmallAutoField"):
72+
if hasattr(fields, field_cls_name):
73+
_fix_id_generator(getattr(fields, field_cls_name))
74+
75+
if USING_DJANGO_3:
8476
def get_prep_value(self, value):
8577
# Json encoding and decoding for spanner is done in python-spanner.
8678
if not isinstance(value, JsonObject) and isinstance(value, dict):

0 commit comments

Comments
 (0)
0