Skip to content

Commit 2d2c56b

Browse files
committed
fix tests
1 parent ca55c97 commit 2d2c56b

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

django_sharding_library/router.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,23 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
7474
return False
7575
model_name = model_name or hints.get('model_name')
7676
model = hints.get('model')
77-
if not model_name and not model:
77+
if model:
78+
model_name = model.__name__
79+
if not model_name:
7880
raise InvalidMigrationException(
7981
'Model name not provided in migration, please pass a `model_name` or `model` with the hints passed into the migration.'
8082
)
8183

8284
# Sometimes, when extending models from another app i.e. the User Model, the app label
8385
# is the app label of the app where the change is defined but to app with the model is
8486
# passed in with the model name.
85-
if not model:
86-
try:
87-
app = apps.get_app_config(app_label)
88-
model = app.get_model(model_name)
89-
except LookupError:
90-
app_label = model_name.split('.')[0]
91-
app = apps.get_app_config(app_label)
92-
model = app.get_model(model_name[len(app_label) + 1:])
87+
try:
88+
app = apps.get_app_config(app_label)
89+
model = app.get_model(model_name)
90+
except LookupError:
91+
app_label = model_name.split('.')[0]
92+
app = apps.get_app_config(app_label)
93+
model = app.get_model(model_name[len(app_label) + 1:])
9394

9495
single_database = self.get_specific_database_or_none(model)
9596
shard_group = self.get_shard_group_if_sharded_or_none(model)

tests/test_router.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def test_requires_model_name_to_be_passed_in(self):
169169
self.sut.allow_migrate(db='default', app_label='tests', **hints)
170170

171171
def test_requires_model_to_be_passed_in(self):
172+
from django.contrib.auth import get_user_model
172173
with self.assertRaises(InvalidMigrationException):
173174
self.sut.allow_migrate(db='default', app_label='tests')
174175

0 commit comments

Comments
 (0)