If you have separate models, you could certainly write the method that
initializes a completely separate sub-class instance from the instance
data of the super class instance. You could also do some magic using
the field list available in the manager (which has the problem, IIRC, that
it is not a public interface) to know which data to copy, though I'm of the
explicit is better than implicit camp.
So, I guess that I really don't know enough about your problem to
suggest a detailed technique. I suspect that if it were me, I'd probably
be using a foreign key reference from Verb to Word, rather than
sub-classing.
Bill
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>
Verb.objects.create(word=word) doesn't fulfill these criteria?
Cheers
Tom
In your database, are there both a Word table and a Verb table? Or is
there just
a (set of) Verb (and Noun and Adjective, etc.) table(s), derived from
a non-Model
Word class?
If there's a Noun instance "record" and a Verb instance "record", do they share
any data (such that if you change it in one it is changed in the
other) or do they
just have duplicate copies of some data?
Bill
class Verb(models.Model):word = models.ForeignKey('Word')# Verb-specific stuff
What you really want to do is to create a python instance of the Verb
class. It's id (in the Verb table) will be empty, meaning the Verb
row will get created on save(). If the field that Verb uses to refer
to Word is already set at save() time, the ORM *MIGHT* just happily
save Verb, and either not save the unmodified Word (I'm pretty sure
that you must have an instantiation of the Word instance lying
around), or, at worst, just update it with unchanged data, rather than
creating a new instance. You'll have to experiment, unless an ORM
expert speaks up.
One problem is how to set that reference in the Verb object. I don't
think you get a 'word' attribute on a Verb object, do you? If worst
comes to worse you can access the attribute by using
v.__dict__['attribute_name'] where v is a Verb instance. You can poke
around in pdb on a Verb instance that was made and already saved in
the normal way to see how things are named and represented.
You are probably on your own to assure that you don't add more than
one Verb to a given Word. Some of the reverse lookup stuff may get
confused if there are more than one. I doubt that the uniqueness
constraint in the DB functions in that direction. Beware multiple
threads - you may need to play with transactions and roll backs.
And, of course, things could change in a future version even if this works now.
Good luck, Bill
I'd sill go with a foreign key field in Verb (and Noun, etc.)
referring to Word. I
think that you can even mark it unique, meaning that only one Verb can
refer to any given Word, but both a Noun and a Verb could refer to the same
Word. But you know your needs.
Bill
There is no issue with Word-s that are both Noun and Verb if both Noun
and Verb have a OneToOneField to Word - the Noun and Verb instances
can point to the same Word instance without any issues whatsoever.
Cheers
Tom
Bill