Actually, it makes a lot of sense, and is considered quite “pythonic”
The==
operator is the Boolean comparison operator in Python. It has the valueTrue
if the two sides of the expression are equal,False
if not equal.
That value (True or False) is what thereturn
statement will return, which is exactly what is required.
Pardon the digression, but as an IT professional and Python teacher, I disagree with your assertion. Clarity and readability are important aspects of writing maintainable code. Using explicit comparisons like return db == 'my_db_alias'
can sometimes be confusing, especially for those unfamiliar with the codebase, so it’s better to provide more descriptive logic whenever possible. This is a best practice that has been around since I first began learning to code back in 1991. Simply labeling a bad practice with a fanciful name, such as “pythonic,” doesn’t somehow make it more acceptable.
Certainly, I have verified that the router is running, by implementing print statements at different points in the method, including one that prints out the parameter values for the current call.
Yes, of course I have visually inspected the database, using pgAdmin, which is why, in my OP, I stated:
When I run the migrations, as specified, NONE of the models in question migrate to the table in question. Instead, all of Django’s built in models migrate over, despite not matching the conditions specified in
allow_migrate
.
Reviewing that statement now, however, I realize that I misspoke. I should have stated that none of the models migrate to the database in question, which is evident by a lack of relevant tables. Somehow, Django’s built-in models are migrating to the database, though, despite not matching any of the conditions specified in my allow_migrate method.
That being said, if I switch my final return statement to false (as below), that resolves the issue of the unwanted migrations. However, the desired migrations are still not being applied, and I have found that returning false can cause issues when multiple routers are being utilized.
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'test_app':
if db == 'tenant_base' and model_name in tenant_models:
return True
return False