Skip to content

Commit 7a636a8

Browse files
authored
[change] Allow passing specific migration number to dependency #43
Closes #43
1 parent 0f84196 commit 7a636a8

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
Verson 1.3.0 [unreleased]
5+
-------------------------
6+
7+
- [change] Allow possibility to point swappable dependency to specific migration number
8+
(instead of only to ``__latest__``)
9+
410
Version 1.2.0 [2021-11-12]
511
--------------------------
612

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function | purpose
200200
`get_model_name(app_label, model)` | Gets the name of the model the swappable model has been swapped for (or the name of the original model if not swapped.)
201201
`get_model_names(app_label, models)` | Match a list of model names to their swapped versions. All of the models should be from the same app (though their swapped versions need not be).
202202
`load_model(app_label, model, required=True)` | Load the swapped model class for a swappable model (or the original model if it hasn't been swapped). If your code can function without the specified model, set `required = False`.
203-
`dependency(app_label, model, latest=False)` | Generate a dependency tuple for use in migrations. Use `latest=True` only when depending on the first migration of the target dependency doesn't work (eg: when all migrations of the target module should be run), please keep in mind that using `latest=True` can have [drawbacks].
203+
`dependency(app_label, model, version=None)` | Generate a dependency tuple for use in migrations. Use `version` only when depending on the first migration of the target dependency doesn't work (eg: when a specific migration needs to be depended upon), we recommend avoid using `version='__latest__'` because it can have serious [drawbacks] when new migrations are added to the module which is being depended upon.
204204
`set_app_prefix(app_label, prefix)` | Set a custom prefix for swappable settings (the default is the upper case `app_label`). This can be useful if the app has a long name or is part of a larger framework. This should be set at the top of your models.py.
205205
`join(app_label, model)`, `split(model)` | Utilities for splitting and joining `"app.Model"` strings and `("app", "Model")` tuples.
206206

swapper/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ def get_model_name(app_label, model):
4444
return is_swapped(app_label, model) or join(app_label, model)
4545

4646

47-
def dependency(app_label, model, latest=False):
47+
def dependency(app_label, model, version=None):
4848
"""
4949
Returns a Django 1.7+ style dependency tuple for inclusion in
5050
migration.dependencies[]
5151
"""
5252
dependencies = swappable_dependency(get_model_name(app_label, model))
53-
if not latest:
53+
if not version:
5454
return dependencies
55-
return dependencies[0], '__latest__'
55+
return dependencies[0], version
5656

5757

5858
def get_model_names(app_label, models):

tests/test_swapper.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ def test_swap_dependency(self):
8686
swapper.dependency("default_app", "Type"), ("alt_app", "__first__")
8787
)
8888
self.assertEqual(
89-
swapper.dependency("default_app", "Type", True), ("alt_app", "__latest__")
89+
swapper.dependency("default_app", "Type", "__latest__"),
90+
("alt_app", "__latest__"),
91+
)
92+
self.assertEqual(
93+
swapper.dependency("default_app", "Type", "0001_custom_migration"),
94+
("alt_app", "0001_custom_migration"),
9095
)
9196

9297
# Tests that only work if default_app.Type is *not* swapped

0 commit comments

Comments
 (0)