Changing an object from one type to another

43 views
Skip to first unread message

Nick

unread,
Jun 13, 2011, 2:07:05 PM6/13/11
to django-polymorphic
Say for example I have:
BaseEmployee(PolymorphicModel):
blah bla blah

SuperEmployee(BaseEmployee):
blah blah balh

BadEmployee(BaseEmployee):
blah blah blah

I have an instance where I can't tell what type of employee the
employee is going to be at creation. This is limited due a constraint
on the data that was given to me.

I want to import all the employees into BaseEmployee

And then provide an admin feature that will let admins of the site
change the type of employee that each employee is.

So I have 100 BaseEmployees...and 20 of them are going to be
SuperEmployees and the remaining 80 will be BadEmployees.
how can I alter each object in BaseEmployee so that it will also show
up in BadEmployee and SuperEmployee queries.

thanks
n

Tribaal

unread,
Jun 15, 2011, 3:38:48 AM6/15/11
to django-po...@googlegroups.com
Hi there,

(Copying the answer I sent you by private email so that this ends up being indexed)

I think there is no "magic casting" method, so the way I would solve this particular problem would be:
1. to have a cast_to_auditor(basemodel) method, that copies the fields over (using introspection it would be pretty simple, for instance).
2. There is a special field on supertype instances called polymorphic_ctype that holds the type of the "sub-instance". I never tried but I assume setting it to the proper type would let polymorphic treat it as a a subtype? This is worth a try.

Let me know how that goes :)

- Chris
Reply all
Reply to author
Forward
0 new messages