|
12 | 12 | # from django.contrib.contenttypes.management import create_contenttypes |
13 | 13 | from django.contrib.contenttypes.models import ContentType |
14 | 14 | from django.core.validators import RegexValidator, ValidationError |
15 | | -from django.db import connection, IntegrityError, models, transaction |
| 15 | +from django.db import connection, connections, IntegrityError, models, transaction |
16 | 16 | from django.db.models import Q |
17 | 17 | from django.db.models.functions import Lower |
18 | 18 | from django.db.models.signals import pre_delete, post_save |
|
52 | 52 |
|
53 | 53 | from netbox_custom_objects.constants import APP_LABEL, RESERVED_FIELD_NAMES |
54 | 54 | from netbox_custom_objects.field_types import FIELD_TYPE_CLASS |
55 | | -from netbox_custom_objects.utilities import generate_model |
| 55 | +from netbox_custom_objects.utilities import generate_model, get_schema_connection |
56 | 56 |
|
57 | 57 |
|
58 | 58 | class UniquenessConstraintTestError(Exception): |
@@ -549,12 +549,11 @@ def create_model(self): |
549 | 549 |
|
550 | 550 | # Ensure the ContentType exists and is immediately available |
551 | 551 | features = get_model_features(model) |
552 | | - self.object_type.features = features + ['branching'] |
553 | | - self.object_type.public = True |
554 | 552 | self.object_type.features = features |
| 553 | + self.object_type.public = True |
555 | 554 | self.object_type.save() |
556 | 555 |
|
557 | | - with connection.schema_editor() as schema_editor: |
| 556 | + with get_schema_connection().schema_editor() as schema_editor: |
558 | 557 | schema_editor.create_model(model) |
559 | 558 |
|
560 | 559 | self.register_custom_object_search_index(model) |
@@ -588,7 +587,7 @@ def delete(self, *args, **kwargs): |
588 | 587 | # TODO: Remove this disconnect/reconnect after ObjectType has been exempted from handle_deleted_object |
589 | 588 | pre_delete.disconnect(handle_deleted_object) |
590 | 589 | object_type.delete() |
591 | | - with connection.schema_editor() as schema_editor: |
| 590 | + with get_schema_connection().schema_editor() as schema_editor: |
592 | 591 | schema_editor.delete_model(model) |
593 | 592 | pre_delete.connect(handle_deleted_object) |
594 | 593 |
|
@@ -936,7 +935,7 @@ def clean(self): |
936 | 935 |
|
937 | 936 | try: |
938 | 937 | with transaction.atomic(): |
939 | | - with connection.schema_editor() as test_schema_editor: |
| 938 | + with get_schema_connection().schema_editor() as test_schema_editor: |
940 | 939 | test_schema_editor.alter_field(model, old_field, model_field) |
941 | 940 | # If we get here, the constraint was applied successfully |
942 | 941 | # Now raise a custom exception to rollback the test transaction |
@@ -1371,7 +1370,7 @@ def save(self, *args, **kwargs): |
1371 | 1370 | model = self.custom_object_type.get_model() |
1372 | 1371 | model_field.contribute_to_class(model, self.name) |
1373 | 1372 |
|
1374 | | - with connection.schema_editor() as schema_editor: |
| 1373 | + with get_schema_connection().schema_editor() as schema_editor: |
1375 | 1374 | if self._state.adding: |
1376 | 1375 | schema_editor.add_field(model, model_field) |
1377 | 1376 | if self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT: |
@@ -1489,7 +1488,7 @@ def delete(self, *args, **kwargs): |
1489 | 1488 | model = self.custom_object_type.get_model() |
1490 | 1489 | model_field.contribute_to_class(model, self.name) |
1491 | 1490 |
|
1492 | | - with connection.schema_editor() as schema_editor: |
| 1491 | + with get_schema_connection().schema_editor() as schema_editor: |
1493 | 1492 | if self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT: |
1494 | 1493 | apps = model._meta.apps |
1495 | 1494 | through_model = apps.get_model(APP_LABEL, self.through_model_name) |
|
0 commit comments