Wednesday, June 22, 2011

Creating an instance of a subclass when superclass instance already exists

Hello all,

I have a model hierarchy in my django app, and there is one very
important task that I do not know how to do. I have a bunch of
instances of the superclass (Word is the model name), and on input
from a user I want to create a subclass instance (Verb) from that
superclass instance. For example, I might have code like this:

>>> word = Word.objects.get(pk=[input from user])
>>> verb = Verb.createFromSuperclassInstance(word)
>>> verb.save()

The only problem is that I know of no such
"createFromSuperclassInstance" method, nor where to look for it. I do
know how to do this through the database itself (just add a row to the
verb table with the correct foreign key), but I was hoping django
would provide some nice way to do it without me having to explicitly
write SQL. The Verb class's __init__ method creates a new Word
instance, so that doesn't work, unless there's a way to use it that I
don't know about.

Any help?

Thanks,
Matt

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment