On Sun, 2007-10-28 at 08:43 +0000, [EMAIL PROTECTED] wrote:
> Hi Malcolm
>   In my design i have a class called publication, that behaves
> different depending on the "state", so i have an state hierarchy that
> implement via polymorphism the different ways the publication behaves.
>   I have been reading the djangobook and searching the net, looking a
> way to implement this without lucky. Maybe i'm asking something a lot
> trivial, sorry me if that is the case.

You can simulate inheritance by using foreign keys or one to one keys.
For example:

        class Parent(models.Model):
            some_data = ...
            ....
        
        class Child(models.Model):
            parent = models.ForeignKey(parent, unique=True)
            ...
        
It's not completely transparent, because you have to manually walk the
link from a child back to the parent (refer to
my_child.parent.some_data, instead of child.some_data as you get in true
inheritance). It's also sometimes a bit fiddly to walk down the
hierarchy: if you have a Parent, finding the associated Child can be
fiddly. When I've done this in the past, I tend to denormalise slightly
and through in a "child_type" field on the Parent model that tells me
which type of child class to query. A little verbose, but you usually
only have to write the "descend the hierarchy" code once and call it
whenever you need it.

Even when we implement model inheritance for the ORM layer, you won't
get truly transparent duck-typing in any case: that is, given a Parent,
you won't automatically know the type of its child because that would
involve O(n) table joins (where n is the number of ancestor types of the
parent -- children, grandchildren, great-grandchildren, etc), so that's
an inefficient search approach (and we're not about to add extra columns
just to store the descendant classes, since that breaks subclassing
third-party models).

So since it sounds like you are going to be taking the Parent instances
and trying to access the class associated with it, it's probably worth
implementing something like a child_type attribute that tells you where
to look. Then when model inheritance is implemented, it won't be too
much of a change to port your code across.

Regards,
Malcolm

> 
> Thanks to you, Sebasti�n
> PD: English is not my natural language, sorry my spell errors.
> 
> On 28 oct, 05:13, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
> > On Sun, 2007-10-28 at 07:09 +0000, [EMAIL PROTECTED] wrote:
> > > Hi
> > >   If i'm not wrong the model inheritance is not complete. How you
> > > turnaround this problem, when you had to implement the state pattern
> > > or the strategy?
> > >   Any idea is welcome!!
> >
> > Could you perhaps ask your question using less buzzwords, since "the
> > state pattern" would mean quite a few things? What are you really trying
> > to do here? A concrete example will help people provide with an
> > appropriate answer.
> >
> > Thanks,
> > Malcolm
> >
> > --
> > Despite the cost of living, have you noticed how popular it 
> > remains?http://www.pointy-stick.com/blog/
> 
> 
> > 
> 
-- 
Borrow from a pessimist - they don't expect it back. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to