craigds/django-typed-models

Recasting from base isn't pretty

Closed this issue · 1 comments

Let's say somehow I get an (unsaved) Animal instance. There isn't a pretty way to recast as a Canine or Feline:

animal = Animal('phoebo')

# This is a no-op thanks to https://github.com/craigds/django-typed-models/blob/master/typedmodels/models.py#L329
animal.recast(Feline)

# This doesn't work either (unless your model defines __str__ to be <app_name>.<model_name>)
animal.type = Feline

# These will work, but isn't ideal
animal.type = 'my_app.Feline'
animal.recast()

# This works too (but only by coincidence, by how recast is implemented)
animal.type = 'nonesense'
animal.recast(Feline)

IMO animal.recast(Feline) should be supported.
(setting the type is smelly, since type is supposed to be a string field)

I might add I run into this issue because I have a Form class which references the base TypedModel. and then override save(). In save() the Form instance has a member instance which is an instance of the model (the base TypedModel). Based on form inputs, I recast the type appropriately and save the instance.