Skip to content

Commit

Permalink
fix issue craigds#65
Browse files Browse the repository at this point in the history
Reverse the MRO list so that we really get the base typed model,
and not the closest subclass.
  • Loading branch information
souliane committed Jan 18, 2023
1 parent aec094f commit ad3b474
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion typedmodels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def __init__(self, *args, _typedmodels_do_recast=None, **kwargs):
self.recast()

def recast(self, typ=None):
for base in self.__class__.mro():
for base in reversed(self.__class__.mro()):
if issubclass(base, TypedModel) and hasattr(base, "_typedmodels_registry"):
break
else:
Expand Down
18 changes: 16 additions & 2 deletions typedmodels/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,34 @@ def test_recast_auto(animals):
assert type(cat) == BigCat


def test_recast_string(animals):
def test_recast_to_subclass_string(animals):
cat = Feline.objects.get(name="kitteh")
cat.recast("testapp.bigcat")
assert cat.type == "testapp.bigcat"
assert type(cat) == BigCat


def test_recast_modelclass(animals):
def test_recast_to_subclass_modelclass(animals):
cat = Feline.objects.get(name="kitteh")
cat.recast(BigCat)
assert cat.type == "testapp.bigcat"
assert type(cat) == BigCat


def test_recast_string(animals):
cat = Feline.objects.get(name="kitteh")
cat.recast("testapp.canine")
assert cat.type == "testapp.canine"
assert type(cat) == Canine


def test_recast_modelclass(animals):
cat = Feline.objects.get(name="kitteh")
cat.recast(Canine)
assert cat.type == "testapp.canine"
assert type(cat) == Canine


def test_recast_fail(animals):
cat = Feline.objects.get(name="kitteh")
with pytest.raises(ValueError):
Expand Down

0 comments on commit ad3b474

Please sign in to comment.